String.join MethodUse the String.join method combine many strings with a delimiter. Benchmark the join method.
This page was last reviewed on Sep 11, 2023.
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 Initializer
String Array
Tip 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.
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); } }
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.
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); } }
Empty delimiter. We can use an empty delimiter string. This is a way to concatenate all the strings together with nothing separating them.
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); } }
Benchmark, 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.
Version 1 This version of the code uses separate string arguments to the String.join method.
Version 2 This code uses an array argument to String.join instead of multiple separate arguments.
Result This benchmark shows that passing an array to String.join is faster than passing separate strings.
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); } }
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.
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.
String split
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Sep 11, 2023 (edit).
© 2007-2023 Sam Allen.