All for Joomla All for Webmasters

Java String join

The java string join() method returns a string joined with given delimiter. In string join method, delimiter is copied for each elements.

In case of null element, “null” is added. The join() method is included in java string since JDK 1.8.

There are two types of join() methods in java string.


Signature

The signature or syntax of string join method is given below:

  1. public static String join(CharSequence delimiter, CharSequence… elements)
  2. and
  3. public static String join(CharSequence delimiter, Iterable<? extends CharSequence> elements)

Parameters

delimiter : char value to be added with each element

elements : char value to be attached with delimiter


Returns

joined string with delimiter


Throws

NullPointerException if element or delimiter is null.


Since

1.8


Java String join() method example

  1. public class StringJoinExample{
  2. public static void main(String args[]){
  3. String joinString1=String.join(“-“,“welcome”,“to”,“makefosure”);
  4. System.out.println(joinString1);
  5. }}

Output:

welcome-to-makeforsure
PinIt
submit to reddit

Leave a Reply

Top