Java Join Strings: String.join Method

This Java article uses the String.join method combine many strings with a delimiter. It provides a benchmark.
Join. In a program, Strings are often separate. They are elements in an array, or just String variables in a method. With join() we merge them.
In Java, the join method receives a variable number of arguments. It accepts first a delimiter string—characters that are inserted between all joined strings.

And: The second and further arguments to join() are the Strings we want to combine together.

First, we call the String.join static method with an array of three Strings. The values array is passed as the second argument to join.Static InitializerString Arrays

Delimiter: The delimiter (or separator value) is three periods here. This can be any character or string.

Result: String.join returns a new String. It contains the merged-together data. No trailing delimiter is added.

Java program that uses String.join method public class Program { public static void main(String[] args) { // Create String array of three elements. String[] values = { "bird", "cat", "wildebeest" }; // Join the elements together. String result = String.join("...", values); System.out.println(result); } } Output bird...cat...wildebeest
Individual string arguments. String.join is versatile. We don't need to pass it an array of Strings—it can make its own array from separate arguments.

Here: We pass the delimiter (a comma) and three values (value1, value2 and value3). Join handles these just like an array.

Java program that joins individual string values public class Program { public static void main(String[] args) { String value1 = "dot"; String value2 = "net"; String value3 = "perls"; // Join the three local variables' data. String joined = String.join(",", value1, value2, value3); System.out.println(joined); } } Output dot,net,perls
Empty delimiter. We can use an empty delimiter string. This is a way to concatenate all the strings together with nothing separating them.
Java program that uses empty delimiter public class Program { public static void main(String[] args) { String[] array = { "A", "B", "C" }; // Join with an empty delimiter. String merged = String.join("", array); System.out.println(merged); } } Output ABC
Performance, arguments. In some programs, we have a choice in how to call String.join—we can change code to use an array or avoid arrays. String.join can handle either case.

However: This benchmark shows that passing an array to String.join is faster than passing separate strings.

So: If you can use an array with String.join, this is a better option. This does not apply to all programs.

Java program that times String.join argument passing public class Program { public static void main(String[] args) { String value1 = "cat"; String value2 = "dog"; String value3 = "elephant"; String[] array = new String[3]; array[0] = "cat"; array[1] = "dog"; array[2] = "elephant"; long t1 = System.currentTimeMillis(); // Version 1: pass separate string arguments. for (int i = 0; i < 10000000; i++) { String result = String.join(":", value1, value2, value3); if (result.length() == 0) { System.out.println(0); } } long t2 = System.currentTimeMillis(); // Version 2: pass array of Strings. for (int i = 0; i < 10000000; i++) { String result = String.join(":", array); if (result.length() == 0) { System.out.println(0); } } long t3 = System.currentTimeMillis(); // ... Result times. System.out.println(t2 - t1); System.out.println(t3 - t2); } } Output 1104 ms: 3 Strings 934 ms: String array
Joiner. Sometimes we need more power when joining strings. With Joiner, a Guava library class, we can use special methods like skipNulls() to improve control.Joiner
Join is a good way to combine strings. With it, we can create CSV (comma-separate values) files. Joined strings can be parsed with split.Split
© 2007-2019 Sam Allen. Every person is special and unique. Send bug reports to info@dotnetperls.com.
HomeSearch
Home
Dot Net Perls