Java StringJoiner
last modified July 5, 2024
In this article we cover Java StringJoiner.
StringJoiner is used to construct a sequence of characters separated by a delimiter and optionally starting with a supplied prefix and ending with a supplied suffix.
StringJoiner
is also used internally by the join
method of the String
class.
Using StringJoiner
The following example joins numbers with the StringJoiner
class.
import java.util.StringJoiner; void main() { var joined = new StringJoiner(","); joined.add("1"); joined.add("2"); joined.add("3"); joined.add("4"); joined.add("5"); System.out.println(joined); }
The example concatenates five numbers and prints the final string to the console.
var joined = new StringJoiner(",");
A new instance of the StringJoiner
class is created. The comma
character is used as a delimiter.
joined.add("1"); joined.add("2"); joined.add("3"); joined.add("4"); joined.add("5");
Five values are added with the add
method.
System.out.println(join);
The StringJoiner
is converted to a string and printed to the
console.
$ java Main.java 1,2,3,4,5
The String.join method
In the second example, we join strings with the String.join
method.
void main() { var joined = String.join("/", "2024", "7", "1"); System.out.println(joined); }
The String.join
method internally uses the StringJoiner
.
var joined = String.join("/", "2024", "7", "1");
A date is concatenated with the String.join
method.
$ java Main.java 2016/8/5
Joining list
The third example concatenates elements of a list.
import java.util.List; void main() { var words = List.of("Today", "is", "a", "beautiful", "day"); var joined = String.join(" ", words); System.out.println(joined); }
A list can be passed as an argument to the String.join
method.
var joined = String.join(" ", words);
The elements of the list are joined with a single space character.
$ java Main.java Today is a beautiful day
Reading CSV file
The following example reads numbers from a CSV file and later joins them with a
StringJoiner
.
13,24,35,16,50
This is the numbers.csv
file.
import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; import java.util.StringJoiner; void main() throws FileNotFoundException { var fileName = "src/main/resources/numbers.csv"; StringJoiner joined; try (var scanner = new Scanner(new File(fileName))) { scanner.useDelimiter(","); joined = new StringJoiner("|"); while (scanner.hasNext()) { joined.add(scanner.next()); } } System.out.println(joined); }
The example reads CSV file, containing numbers, and joins them with a
StringJoiner
using a different delimiter.
try (var scanner = new Scanner(new File(fileName))) { scanner.useDelimiter(",");
The values are read with the Scanner
class. The numbers are
separated by a comma character so we set the comma delimiter with the
useDelimiter
method.
var joined = new StringJoiner("|");
A StringJoiner
class is instantiated with a "|" delimiter.
while (scanner.hasNext()) { join.add(scanner.next()); }
We retrieve the values with the scanner and concatenate them with the joiner.
Writing CSV file
The next example writes numbers to a CSV file.
import java.io.File; import java.io.IOException; import java.io.PrintWriter; import java.util.StringJoiner; void main() throws IOException { var fileName = "src/main/resources/numbers2.csv"; var joined = new StringJoiner(","); joined.add("21"); joined.add("43"); joined.add("54"); joined.add("76"); joined.add("98"); var newFile = new File(fileName); boolean created = newFile.createNewFile(); if (created) { try (var pw = new PrintWriter(newFile)) { pw.write(joined.toString()); } } }
The example joins five numbers with a StringJoiner
and writes the
concatendated string to a CSV file.
var joined = new StringJoiner(","); joined.add("21"); joined.add("43"); joined.add("54"); joined.add("76"); joined.add("98");
Five numbers are concatenated with the StringJoiner
. The numbers
are separated with a comma character.
var newFile = new File(fileName); boolean created = newFile.createNewFile();
A new file object is created in the current working directory.
if (created) { try (var pw = new PrintWriter(newFile)) { pw.write(joined.toString()); } }
The joined values are written to the file.
The Collectors.joining method
Tthe Collectors.joining
method returns a Collector
that concatenates the input elements, separated by the specified delimiter, in
encounter order.
import java.util.stream.Collectors; import java.util.stream.Stream; void main() { Stream<String> stream = Stream.of("Jan", "Peter", "Robert"); String names = stream.collect(Collectors.joining(" ")); System.out.println(names); }
The example uses the stream API to concatenate three names.
$ java Main.java Jan Peter Robert
Source
Java StringJoiner - language reference
In this article we have covered StringJoiner
and
Collectors.joining
.
Author
List all Java tutorials.