2020-04-13

Java String join() method

Java String join() method


String join() method added java 8 (jdk8).

This method Returns a new String composed of copies of the CharSequence elements joined together with a copy of the specified delimiter.

method:

public static String join​(CharSequence delimiter, CharSequence... elements);

delimiter - the delimiter that separates each element
elements - the elements to join together.

Returns:
a new String that is composed of the elements separated by the delimiter

Throws:
NullPointerException - If delimiter or elements is null

Java Program Example:


class StringJoin{
public static void main(String args[]){
String message = String.join("-", "Java", "is", "cool");
     // message returned is: "Java-is-cool"

System.out.println(message);
}
}

output:

Java-is-cool

No comments:

Post a Comment