Java 字符串拼接

Java字符串拼接

String join()介绍

join() 方法返回使用指定分隔符拼接一个字符串。在join() 方法中,为每个元素添加了分隔符。如果为null元素,则添加“null”。从JDK 1.8开始,Java字符串才包含join() 方法。

Java字符串 两种join()方法

delimiter:每个元素要添加的分隔符

elements:需要添加分隔符的字符串

join(CharSequence delimiter, CharSequence... elements)

public static String join(CharSequence delimiter, CharSequence... elements) {
		//判断是否为null,如果为null,抛出NullPointerException
        Objects.requireNonNull(delimiter);
        Objects.requireNonNull(elements);
        // Number of elements not likely worth Arrays.stream overhead.
        //构造一个分隔符为delimiter的实例   
        StringJoiner joiner = new StringJoiner(delimiter);
        //循环拼接   
        for (CharSequence cs: elements) {
            joiner.add(cs);
        }
        return joiner.toString();
    }
join(CharSequence delimiter,  Iterable<? extends CharSequence> elements)

public static String join(CharSequence delimiter, Iterable<? extends CharSequence> elements) {
		//判断是否为null,如果为null,抛出NullPointerException
        Objects.requireNonNull(delimiter);
        Objects.requireNonNull(elements);
         //构造一个分隔符为delimiter的实例 
        StringJoiner joiner = new StringJoiner(delimiter);
        //循环拼接   
        for (CharSequence cs: elements) {
            joiner.add(cs);
        }
        return joiner.toString();
    }

测试

多个字符串

public class StringJoinTest {
  public static void main(String[] args) {
    String str = String.join("-","A","B","C");
    System.out.println("str : "+str);
  }
}

Java 字符串拼接_第1张图片
集合

public class StringJoinTest {
  public static void main(String[] args) {
      List<String> stringList = new ArrayList<String>();
      stringList.add("AAA");
      stringList.add("BBB");
      stringList.add("CCC");
      String str =String.join("-",stringList);
      System.out.println("str : "+str);
  }
}

Java 字符串拼接_第2张图片
数组

public class StringJoinTest {
  public static void main(String[] args) {
      String[] strs = new String[]{"AAA","BBB","CCC"};
      String str =String.join("-",strs);
      System.out.println("str : "+str);
  }
}

Java 字符串拼接_第3张图片

StringUtils.join()介绍

StringUtils.join()用途:StringUtils.join()与String join()类似,都是可以将数组或集合以某拼接符拼接到一起形成新的字符串。
注意:使用前需先引入common-lang3的jar包

测试

public class StringJoinTest {
  public static void main(String[] args) {
      List<String> stringList = new ArrayList<String>();
      stringList.add("AAA");
      stringList.add("BBB");
      stringList.add("CCC");
      String str = StringUtils.join(stringList,"-");
      System.out.println("str : "+str);
      
      String[] strs = new String[]{"AAA","BBB","CCC"};
      String str2 = StringUtils.join(strs,"-");
      System.out.println("str2 : "+str2);
  }
}

Java 字符串拼接_第4张图片

StringJoiner 介绍

StringJoiner是Java8新出的一个类,用于构造由分隔符分隔的字符序列,并可选择性地从提供的前缀开始和以提供的后缀结尾。

//delimiter 分隔符
//prefix 前缀
//suffix 后缀
public StringJoiner(CharSequence delimiter,  CharSequence prefix,  CharSequence suffix) {
		//判断是否为null,如果为null,抛出NullPointerException
        Objects.requireNonNull(prefix, "The prefix must not be null");
        Objects.requireNonNull(delimiter, "The delimiter must not be null");
        Objects.requireNonNull(suffix, "The suffix must not be null");
        // make defensive copies of arguments
        //为成员变量赋值
   	    //前缀
        this.prefix = prefix.toString();       
    	//分隔符
        this.delimiter = delimiter.toString();        
   		//后缀    
        this.suffix = suffix.toString();
    }

//String.join()方法中是通过add方法拼接字符串
public StringJoiner add(CharSequence newElement) {
        final String elt = String.valueOf(newElement);
        if (elts == null) {
            elts = new String[8];
        } else {
            if (size == elts.length)
                elts = Arrays.copyOf(elts, 2 * size);
            len += delimiter.length();
        }
        len += elt.length();
        elts[size++] = elt;
        return this;
    }

测试

public class StringJoinerTest {
  public static void main(String[] args) {
      StringJoiner stringJoiner = new StringJoiner("-");
      stringJoiner.add("AAA");
      stringJoiner.add("BBB");
      stringJoiner.add("CCC");
      System.out.println("stringJoiner : "+stringJoiner);
  }
}

Java 字符串拼接_第5张图片

public class StringJoinerTest {
  public static void main(String[] args) {
     /* StringJoiner stringJoiner = new StringJoiner("-");
      stringJoiner.add("AAA");
      stringJoiner.add("BBB");
      stringJoiner.add("CCC");
      System.out.println("stringJoiner : "+stringJoiner);*/

      StringJoiner stringJoiner = new StringJoiner("-","<",">");
      stringJoiner.add("AAA");
      stringJoiner.add("BBB");
      stringJoiner.add("CCC");
      System.out.println("stringJoiner : "+stringJoiner);
  }
}

Java 字符串拼接_第6张图片

你可能感兴趣的:(学习,笔记,Java,字符串)