1. String.join() Method
The String.join() method takes the first argument as a delimiter. In the second argument, we can pass either multiple strings or some instance of Iterable having strings to concatenate. The join() will return a new String that is composed of the strings separated by the delimiter.
The method is an overloaded method and it can concatenate either multiple strings passed as varargs or as List.
static String join(CharSequence delimiter, CharSequence... elements) static String join(CharSequence delimiter, Iterable<? extends CharSequence> elements)
2. String.join() Example
Let us see the example of both variations of the method. First, we will concatenate the strings passed as varargs.
String joined = String.join("/","usr","local","bin");
The program output.
Next, we are joining a List of strings.
String ids = String.join(", ", ZoneId.getAvailableZoneIds());
The program output:
Asia/Aden, America/Cuiaba, Etc/GMT+9, Etc/GMT+8....
So next time, we can use String.join() method for concatenating the strings with a delimiter. It is intended to be used in simple cases. For complex usages, use StringJoiner class.
Happy Learning !!