Java: String join() method

In this article, we will discuss how to join all strings using delimiter specified in the String’s join() method

 

join() method: This String method joins all strings using specified delimiter and finally returns a composed string (or say concatenated string with delimiter)

Note:

  • there are 2 variants or overloaded join() methods
  • in both methods if the elements specified is null, then null is added

 

Method Signature:

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

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

 

Parameters:

  • delimiter –> this is used to separate each elements (each strings)
  • elements –> elements which will be joined together using delimiter

 

Returns:

join() method Returns
join(CharSequence delimiter, CharSequence… elements); Returns all elements joined together with delimiter in between all strings (i.e.; new composed string)
join(CharSequence delimiter, Iterable<? extends CharSequence> elements); Returns all elements present in collection object joined together with delimiter in between (i.e.; new composed string from List or Set)

Note: this is newly introduced in Java 1.8 version

 

Throws:

  • String’s join() method throws NullPointerException, if character sequence (or elements) or delimiter is NULL

 

Examples on join() method:

Example 1: Java program to join all string elements (with specified delimiter in between)

Method Signature:

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

 

StringJoinMethodForVarargs.java

package in.bench.resources.string.methods;

public class StringJoinMethodForVarargs {

	public static void main(String[] args) {

		// Example-1 : message returned is: "BenchResources.Net-is-a-Java-weblog"
		// hyphen (-) is a delimiter
		String strDescForMessage = 
				String.join("-", "BenchResources.Net", "is", "a", "Java", "weblog");

		// print to console
		System.out.println("The returned string message is : " + strDescForMessage);


		// Example-2 : message returned is: "www.BenchResources.Net"
		// dot or period (.) is a delimiter
		String strDescForWebUrl = 
				String.join(".", "www", "BenchResources", "Net");

		// print to console
		System.out.println("The returned string web url is : " + strDescForWebUrl);


		// Example-3 : message returned is: "30/07/2014"
		// forward slash (/) is a delimiter
		String strDescForDate = 
				String.join("/", "30", "07", "2014");

		// print to console
		System.out.println("The returned string date is : " + strDescForDate);
	}
}

Output:

The returned string message is : BenchResources.Net-is-a-Java-weblog
The returned string web url is : www.BenchResources.Net
The returned string date is : 30/07/2014

 

Example 2: Java program to join all string elements present inside collection classes (with specified delimiter in between)

Method Signature:

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

 

StringJoinMethodForCollection.java

package in.bench.resources.string.methods;

import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

public class StringJoinMethodForCollection {

	public static void main(String[] args) {

		// create ArrayList for adding string elements
		List<String> strList = new ArrayList<>();

		// add elements to List
		strList.add("BenchResources.Net");
		strList.add("is");
		strList.add("a");
		strList.add("Java");
		strList.add("weblog");

		// Example-1 : message returned is: "BenchResources.Net-is-a-Java-weblog"
		// converting List to String using hyphen (-) as delimiter
		String strMessage = String.join(" ", strList);

		// print to console
		System.out.println("The returned string message is : " + strMessage);


		// create LinkedHashSet for adding string elements
		Set<String> strSet = new LinkedHashSet<String>();

		// add elements to Set
		strSet.add("www");
		strSet.add("BenchResources");
		strSet.add("Net");

		// Example-2 : message returned is: "www.BenchResources.Net"
		// converting Set to String using dot/period (.) as delimiter
		String strWebUrl = String.join(".", strSet);

		// print to console
		System.out.println("The returned string web url is : " + strWebUrl);
	}
}

Output:

The returned string message is : BenchResources.Net is a Java weblog
The returned string web url is : www.BenchResources.Net

 

Example 3: Java program to join all string elements when null is present as one such element

StringJoinMethodForNull.java

package in.bench.resources.string.methods;

public class StringJoinMethodForNull {

	public static void main(String[] args) {

		// Example-1 : message returned is: "Java-has-null-element"
		// hyphen (-) is a delimiter
		String strDescForMessage = 
				String.join("-", "Java", "has", null, "element");

		// print to console
		System.out.println("The returned string message with NULL is : " + strDescForMessage);
	}
}

Output:

The returned string message with NULL is : Java-has-null-element

 

Example 4: Java program to join all string elements when delimiter is null

StringJoinMethodForNullDelimiter.java

package in.bench.resources.string.methods;

public class StringJoinMethodForNullDelimiter {

	public static void main(String[] args) {

		// Example-1 : NPE thrown
		// null as delimiter
		String strDescForMessage = 
				String.join(null, "test", "this", "string");

		// print to console
		System.out.println("The returned string message with NULL is : " + strDescForMessage);
	}
}

Output:

Exception in thread "main" java.lang.NullPointerException
	at java.util.Objects.requireNonNull(Objects.java:203)
	at java.lang.String.join(String.java:2451)
	at in.bench.resources.string.methods.StringJoinMethodForNullDelimiter
.main(StringJoinMethodForNullDelimiter.java:10)

 

References:

https://docs.oracle.com/javase/tutorial/essential/concurrency/immutable.html
https://docs.oracle.com/javase/tutorial/essential/concurrency/imstrat.html
https://docs.oracle.com/javase/tutorial/java/data/strings.html
https://docs.oracle.com/javase/6/docs/api/java/lang/String.html
https://docs.oracle.com/javase/7/docs/api/java/lang/String.html
https://docs.oracle.com/javase/8/docs/api/java/lang/String.html
https://docs.oracle.com/javase/6/docs/api/java/lang/class-use/String.html
https://docs.oracle.com/javase/7/docs/api/java/lang/class-use/String.html
https://docs.oracle.com/javase/8/docs/api/java/lang/class-use/String.html
https://docs.oracle.com/javase/6/docs/api/java/lang/StringBuffer.html
https://docs.oracle.com/javase/7/docs/api/java/lang/StringBuffer.html
https://docs.oracle.com/javase/8/docs/api/java/lang/StringBuffer.html
https://docs.oracle.com/javase/6/docs/api/java/lang/StringBuilder.html
https://docs.oracle.com/javase/7/docs/api/java/lang/StringBuilder.html
https://docs.oracle.com/javase/8/docs/api/java/lang/StringBuilder.html

 

Happy Coding !!
Happy Learning !!

Java: String lastIndexOf() method
Java: String isEmpty() method