This post will discuss how to join multiple strings in Java (specified as an array, Iterable, varargs, etc.) with a delimiter.

1. Using Loop

The idea is to loop through the collection and append each element to a string separated by a delimiter. The variable prefix is used to avoid adding a delimiter at the end of the output.

Download  Run Code

Output:

A,B,C,D

2. Using Java 8 String.join() method

From Java 8 onward, we can use the String.join() method, which joins strings together with a specified separator. This method only works on Iterable and varargs.

Download  Run Code

Output:

A,B,C,D

3. Using Guava’s Joiner Class

Like the String.join() method, Guava provides Joiner class, which joins elements of a collection (say, iterable, iterator, or Object[]) with a separator.

Download Code

Output:

A,B,C,D

That’s all about joining multiple strings in Java using a delimiter.