Java String join() With Examples

Java String join() function is used to join multiple strings to create a new string with the specified delimiter.

Java String join()

Java String join() method is added in Java 8 release. There are two variants of join() method, both of them are static methods.

The first argument is the delimiter string to use when joining the strings. CharSequence is an interface and some well known implementations are String, StringBuffer and StringBuilder.

Let’s look at these methods with some example.

Output: A,B,C,D

Notice that I have used StringBuffer and StringBuilder as elements to join since they implement CharSequence interface.

Let’s look at an example where we will pass an Iterable whose elements will be joined with the given delimiter.

Output: Hello World 2019

We can execute above code snippets using jshell terminal too.

java-string-join

Java String join() Example – jshell

Reference: API Doc

By admin

Leave a Reply