通過StringUtils的join函數(shù)直接獲取逗號隔開字符串
List<String> test =new ArrayList<>();
test.add("xiaoming");
test.add("xiaohong");
test.add("hhh");
String join = StringUtils.join(test, ",");
System.out.println(join);
最近用到很多字符串拼接的場景,自己做一點(diǎn)梳理和記錄,方便使用的時(shí)候獲取。
1、將list<String>轉(zhuǎn)為逗號隔開字符串
1)只要不為空,就在后面添加逗號,最后再用subString()函數(shù)截取,去掉最后一位的逗號
?List<String> test = new ArrayList<>();
? ? ? ? String testStrings = "";
? ? ? ? test.add("xiaoming");
? ? ? ? test.add("xiaohong");
? ? ? ? test.add("hhh");
? ? ? ? for (String object : test) {
? ? ? ? ? ? if (object != null) {
? ? ? ? ? ? ? ? testStrings += object.toString() + ",";
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? if (testStrings.length() > 0) {
? ? ? ? ? ? testStrings = testStrings.substring(0, testStrings.length() - 1);
? ? ? ? }
2)通過StringUtils的join函數(shù)直接獲取逗號隔開字符串
List<String> test =new ArrayList<>();
test.add("xiaoming");
test.add("xiaohong");
test.add("hhh");
String join = StringUtils.join(test, ",");
System.out.println(join);
?
2、將逗號連接的字符串轉(zhuǎn)成字符數(shù)組:
通過String的split函數(shù)可以實(shí)現(xiàn)
String aaa = "aa,bb,cc";
String[] split = aaa.split(",");
3、將字符數(shù)組String[]轉(zhuǎn)成list<string>
通過Arrays.asList函數(shù)實(shí)現(xiàn)文章來源:http://www.zghlxwxcb.cn/news/detail-486916.html
?
————————————————
版權(quán)聲明:本文為CSDN博主「豆芽爸爸呀」的原創(chuàng)文章,遵循CC 4.0 BY-SA版權(quán)協(xié)議,轉(zhuǎn)載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/zhangxingyu126/article/details/108268840文章來源地址http://www.zghlxwxcb.cn/news/detail-486916.html
到了這里,關(guān)于java將list轉(zhuǎn)為逗號隔開字符串的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!