「这是我参与11月更文挑战的第3天,活动详情查看:2021最后一次更文挑战」
Joiner拼接List中的字符串
List<String> strList = Arrays.asList("java", "go", "c++", "c#");
String str = Joiner.on("$").join(strList);
拼接字符串过滤null值
List<String> strListContainNull = Arrays.asList("java", "go", "c++", null, "c#");
String strContainNull = Joiner.on("$").skipNulls().join(strListContainNull);
拼接的时候替换null值
List<String> strListReplaceNull = Arrays.asList("java", "go", "c++", null, "c#");
String strReplaceNull = Joiner.on("$").useForNull("default_null").join(strListReplaceNull);
直接拼接到StringBuilder或者StringBuild中
StringBuilder sb = new StringBuilder("hello,hello,hello");
StringBuilder newSb = Joiner.on("$").appendTo(sb, strList);
System.out.println(sb.toString());
System.out.println(newSb);
System.out.println(sb == newSb);
直接写入文件中
try (FileWriter writer = new FileWriter(new File("D:\test.txt"))) {
Joiner.on("$").appendTo(writer, strList);
} catch (IOException e) {
e.printStackTrace();
}
Joiner拼接map
Map<String,String> map = new HashMap<>();
map.put("hello","java");
map.put("hi","c++");
String strMap = Joiner.on("#").withKeyValueSeparator("-").join(map);