StringBuilder,StringJoiner,String.join三种不同拼接字符串方法
程序员文章站
2021-12-25 10:56:50
...
三种不同的拼接字符串方法
一、StringBuilder拼接字符串方法
StringBuilder sb=new StringBuilder();
sb.append("你好");
sb.append(",");
sb.append("Ava");
System.out.println(sb);
二、StringJoiner拼接字符串方法
//以逗号隔开
StringJoiner stringJoiner=new StringJoiner(",");
stringJoiner.add("zhangsan");
stringJoiner.add("lisi");
System.out.println(stringJoiner);
//开头和结尾用[]包起来
StringJoiner sj1=new StringJoiner(",","[", "]");
sj1.add("你好");
sj1.add("张三");
sj1.add("wfwef");
System.out.println(sj1);
StringJoiner stringJoiner=new StringJoiner(",");
stringJoiner.add("zhangsan");//新增
stringJoiner.add("lisi");
System.out.println(stringJoiner);
StringJoiner sj=new StringJoiner(",");
sj.add("www");
stringJoiner.merge(sj);//合并在一起
/*stringJoiner.setEmptyValue(" ");*/ //如果stringJoiner对象为空,设置为“ ”值
stringJoiner.add("ppp");
System.out.println(stringJoiner);
三、String.join方法
String st=String.join(",","hello","讲人话");//以逗号隔开拼接
System.out.println(st);
上一篇: JDK1.8特性: Stream 流对象
下一篇: lnmp论坛文档