Join or Concatenate Strings with Comma in Java

This tutorial contains Java examples to join or concatenate a string array to produce a single string using comma delimiter 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. Java 8 – Using String.join()

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

  • The first version joins multiple string literals provided as var-args.
  • The second version joins the strings provided in a list or an array.

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 String Literals

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 join multiple string literals that are not yet in the form of a collection or array.

String joinedString = String.join(",", "How", "To", "Do", "In", "Java");  //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);   //How,To,Do,In,Java

2. Java 8 – Using StringJoiner for Formatted Output

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")
      .add("To")
      .add("Do")
      .add("In")
      .add("Java")
      .toString();

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

3. Java 8 – Using Collectors.joining() for Streams

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 = tokens.stream()
      .collect(Collectors.joining(",", "[", "]"));

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

4. Apache Commons – Using StringUtils.join()

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.

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.12.0</version>
</dependency>

See the 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 strings with comma or any other delimiter in Java.

Happy Learning !!

Comments

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

About Us

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

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

Our Blogs

REST API Tutorial

Dark Mode

Dark Mode