java 1.8新类
类中方法(public的):
两个构造方法:
StringJoiner(CharSequence)
构造方法1: 只有拼接符
StringJoiner(CharSequence,CharSequence,CharSequence )
构造方法2: 拼接符和前后缀
add(CharSequence) :StringJoiner
进行拼接的方法
merge(StringJoiner :StringJoiner
两个StringJoiner可以进行合并
setEmptyValue(CharSequence):StringJoiner
设置 如果stringJoiner是空的默认值
toString():String
返回值变成String类型
length():int
返回StringJoiner的长度
主要示例代码:
public static void main(String[] args) {
StringJoiner stringJoiner = new StringJoiner(";","[","]");
String color1 = "red";
String color2 = "green";
String color3 = "black";
stringJoiner.add(color1);
stringJoiner.add(color2);
stringJoiner.add(color3);
System.out.println(stringJoiner);// 结果为 [red;green;black]
}
public static void main(String[] args) {
StringJoiner stringJoiner = new StringJoiner(";");
String color1 = "red";
String color2 = "green";
String color3 = "black";
stringJoiner.add(color1);
stringJoiner.add(color2);
stringJoiner.add(color3);
System.out.println(stringJoiner); // 结果为 red;green;black
}
public static void main(String[] args) {
StringJoiner stringJoiner = new StringJoiner(";");
String color1 = "red";
String color2 = "green";
String color3 = "black";
stringJoiner.add(color1);
stringJoiner.add(color2);
stringJoiner.add(color3);
StringJoiner stringJoiner1 = new StringJoiner(";");
String color4 = "blue";
String color5 = "white";
String color6 = "yellow";
stringJoiner1.add(color4);
stringJoiner1.add(color5);
stringJoiner1.add(color6);
StringJoiner result = stringJoiner.merge(stringJoiner1);
System.out.println(result); // 结果为 red;green;black;blue;white;yellow
}
public static void main(String[] args) {
StringJoiner stringJoiner = new StringJoiner(";");
stringJoiner.setEmptyValue("color");
System.out.println(stringJoiner); // 结果为 color
}