欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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 jdk1.8