java.join() 用中间字符拼接字符串

java.join 用中间字符拼接字符串

    • 一般字符
    • 字符泛型集合

一般字符


	//String.join(CharSequence delimiter, CharSequence elements);
	String message = String.join("-", "Hello", "World", "!");
	// message returned is: "Hello-World-!"

字符泛型集合


	//join(CharSequence delimiter,Iterable elements)
	List<String> strings = new LinkedList<>();
    strings.add("Hello");strings.add("World");
    strings.add("!");
    String message = String.join(" ", strings);
    //message returned is: "Hello World !"

    Set<String> strings = new LinkedHashSet<>();
    strings.add("Hello"); strings.add("World");
    strings.add("World"); strings.add("Hello");
    strings.add("!");
    String message = String.join("-", strings);
    //message returned is: "Hello-World-World-Hello-!"
    

你可能感兴趣的:(java,字符串)