StringJoiner用于构造存在分隔符的字符串,可以指定字符串的前缀、后缀。
通过setEmptyValue()方法设置字符串为空时的返回值,即使指定了前缀后缀,只要字符串为空,也返回etEmptyValue()方法设置的值。
merge()方法,返回值为【调用对象的前缀+调用对象的字符串值+入参对象的字符串值+调用对象的后缀】。
StringJoiner sj1 = new StringJoiner(",");
System.out.println("sj1 = " + sj1); // sj1 =
StringJoiner sj2 = new StringJoiner(",", "{", "}");
sj2.add("ab").add("c");
System.out.println("sj2 = " + sj2); // sj2 = {ab,c}
StringJoiner sj3 = new StringJoiner(",", "{", "}");
sj3.setEmptyValue("eee");
System.out.println("sj3 = " + sj3); // sj3 = eee
StringJoiner sj4 = new StringJoiner(",");
sj4.add("a").add("b");
System.out.println("sj4 = " + sj4); // sj4 = a,b
System.out.println("sj4.length() = " + sj4.length()); // 3
StringJoiner sj5 = new StringJoiner("-", "[", "]");
sj5.add("xy").add("z");
System.out.println("sj5 = " + sj5); // sj5 = [xy-z]
StringJoiner sj11 = new StringJoiner(",", "{", "}").add("ab").add("c");;
StringJoiner sj12 = new StringJoiner("-", "[", "]").add("xy").add("z");;
System.out.println("{ab,c} merge [xy-z] = " + sj11.merge(sj12)); // {ab,c} merge [xy-z] = {ab,c,xy-z}
StringJoiner sj21 = new StringJoiner(",", "{", "}").add("ab").add("c");
StringJoiner sj22 = new StringJoiner("-", "[", "]").add("xy").add("z");;
System.out.println("[xy-z] merge {ab,c} = " + sj22.merge(sj21)); // [xy-z] merge {ab,c} = [xy-z-ab,c]