GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing zetcode.com — Java StringJoiner - joining strings in Java Java StringJoinerlast modified May 31, 2025
This article explores StringJoiner helps format sequences of characters by inserting a delimiter between elements. Additionally, it allows customization with an optional prefix and suffix, making it ideal for structured text formatting.
One important detail about
Internally, Using StringJoiner
The following example joins numbers with the Main.java
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 joined.add("1");
joined.add("2");
joined.add("3");
joined.add("4");
joined.add("5");
Five values are added with the System.out.println(join);
The $ java Main.java 1,2,3,4,5 The String.join method
In the second example, we join strings with the Main.java
void main() {
var joined = String.join("/", "2024", "7", "1");
System.out.println(joined);
}
The var joined = String.join("/", "2024", "7", "1");
A date is concatenated with the $ java Main.java 2016/8/5 Joining listThe third example concatenates elements of a list. Main.java
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 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
data.csv
13,24,35,16,50
This is the Main.java
void main() throws FileNotFoundException {
var fileName = "data.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
try (var scanner = new Scanner(new File(fileName))) {
scanner.useDelimiter(",");
The values are read with the var joined = new StringJoiner("|");
A while (scanner.hasNext()) {
join.add(scanner.next());
}
We retrieve the values with the scanner and concatenate them with the joiner. Writing CSV fileThe next example writes numbers to a CSV file. Main.java
void main() throws IOException {
var fileName = "data.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 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 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
The Main.java
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 SourceJava StringJoiner - language reference
In this article we have covered AuthorMy name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming. List all Java tutorials. |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |