Join String Array Items with Delimiter

This tutorial contains Java examples to join the items in a string array to produce a single string where items will be separated by a given separator. This code can be used to convert an array to a comma-separated string in Java. We may need this information many times during development especially while parsing contents of the JSON or XML files.

1. Join String Array with String.join() – Java 8

The String.join() method has two overloaded forms.

  • The first version joins multiple strings provided separately.
  • The second version joins the strings provided in lists or arrays.

Note that if an element is null, then "null" is added to the joined string.

static String join(CharSequence delimiter, CharSequence... elements)

static String join(CharSequence delimiter, Iterable<? extends CharSequence> elements)

1.1. Join Multiple Strings

This method takes all strings in var-args format and all strings are passed as arguments in the method. The return string is received by appending all strings delimited by an argument delimiter.

This method can be used to join multiple strings which are not yet in form of a collection or array.

String joinedString = String.join(", ", "How", "To", "Do", "In", "Java");
System.out.println(joinedString);  //How, To, Do, In, Java

1.2. Joining Array or List of Strings

We can use this method to join the string items in the array to produce a joined string.

String[] strArray = { "How", "To", "Do", "In", "Java" };

String joinedString = String.join(", ", strArray);

System.out.println(joinedString);  //How, To, Do, In, Java

2. StringJoiner for Formatted Output – Java 8

Using StringJoiner class, we can produce formatted output of joined strings. This is specially useful while using lambda collectors.

2.1. Method Syntax

The constructors take three arguments – delimiter [mandatory], and optionally prefix and suffix.

StringJoiner(CharSequence delimiter)
StringJoiner(CharSequence delimiter, CharSequence prefix, CharSequence suffix)

2.2. StringJoiner Example

Run the example with similar input as above example to join multiple strings. We want to format the output as [How, To, Do, In, Java], then we can use the below code:

    StringJoiner joiner = new StringJoiner(", ", "[", "]");

    String joinedString = joiner.add("How")

    System.out.println(joinedString); //[How, To, Do, In, Java]

3. Collectors.joining() – Java 8

While using lambda expressions, we can use Collectors.joining() to collect the list items into a String.

    List<String> tokens = Arrays.asList("How", "To", "Do", "In", "Java");

    String joinedString =
      .collect(Collectors.joining(", ", "[", "]"));

    System.out.println(joinedString);  //[How, To, Do, In, Java]

4. StringUtils.join() – Apache Commons

The StringUtils class of the Apache Commons Langs library has several join() methods that can be used to combine an array or list of strings into a single string.


See given examples.

  • In the first example, we are joining a string array with an empty delimiter.
  • In the second example, we are joining a string array with a comma delimiter.
String[] strArray = { "How", "To", "Do", "In", "Java" };

String joinedString = StringUtils.join(strArray);
System.out.println(joinedString);   //HowToDoInJava

String joinedString2 = StringUtils.join(strArray, ", ");
System.out.println(joinedString2);  //How, To, Do, In, Java

Use the above-given examples to concatenate string array in Java.

Happy Learning !!

Was this post helpful?

Join 8000+ Awesome Developers, Like YOU!

2 thoughts on “Join String Array Items with Delimiter”

  1. Hello! I saw in the example 2 the following:

    String joinedString = numbers
    .map(i -> i.toString())…..

    In this case I think that the map(..) is unnecessary because the collection is already a string.


Leave a Comment

About HowToDoInJava

This blog provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions, and frequently asked interview questions.