Efficient String Concatenation with StringJoiner and String.join in Java


Both StringJoiner and String.join offer efficient ways for concatenating strings with delimiters, with StringJoiner providing additional flexibility for prefixes and suffixes.

Source Code

StringJoiner sj = new StringJoiner(", ", "{", "}");
sj.add("one").add("two").add("three");
System.out.println(sj.toString()); // Outputs: {one, two, three}

String result = String.join("-", "2017", "10", "31");
System.out.println(result); // Outputs: 2017-10-31
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments