Write a program that joins the given strings by the given delimiter.
We will show four ways to join the given strings by the given delimiter n Java:
- Join multiple strings via Java 8, String.join()
- Join multiple strings via StringBuilder
- Join multiple strings via Java 8, Collectors.joining()
- Join multiple strings via Java 8, StringJoiner
Java program to Join multiple strings with a delimiter in Java
import java.util.Arrays;
import java.util.StringJoiner;
import java.util.stream.Collectors;
public final class Strings {
public static String joinByDelimiterV1(char delimiter, String... args) {
if (args == null || args.length == 0) {
// or throw IllegalArgumentException
return "";
}
StringBuilder result = new StringBuilder();
int i = 0;
for (i = 0; i < args.length - 1; i++) {
result.append(args[i]).append(delimiter);
}
result.append(args[i]);
return result.toString();
}
public static String joinByDelimiterV2(char delimiter, String... args) {
if (args == null || args.length == 0) {
// or throw IllegalArgumentException
return "";
}
return Arrays.stream(args, 0, args.length)
.collect(Collectors.joining(String.valueOf(delimiter)));
}
public static String joinByDelimiterV3(char delimiter, String... args) {
if (args == null || args.length == 0) {
// or throw IllegalArgumentException
return "";
}
StringJoiner joiner = new StringJoiner(String.valueOf(delimiter));
for (String arg : args) {
joiner.add(arg);
}
return joiner.toString();
}
}
Test
public class Main {
private static final String TEXT_1 = "Illinois";
private static final String TEXT_2 = "Mathematics";
private static final String TEXT_3 = "and";
private static final String TEXT_4 = "Science";
private static final String TEXT_5 = "Academy";
public static void main(String[] args) {
System.out.println("Join multiple string via Java 8, String.join():");
String stringV1 = String.join(" ", TEXT_1, TEXT_2, TEXT_3, TEXT_4, TEXT_5);
System.out.println("String: " + stringV1);
System.out.println();
System.out.println("Join multiple string via StringBuilder:");
String stringV2 = Strings.joinByDelimiterV1(' ', TEXT_1, TEXT_2, TEXT_3, TEXT_4, TEXT_5);
System.out.println("String: " + stringV2);
System.out.println();
System.out.println("Join multiple string via Java 8, Collectors.joining():");
String stringV3 = Strings.joinByDelimiterV2(' ', TEXT_1, TEXT_2, TEXT_3, TEXT_4, TEXT_5);
System.out.println("String: " + stringV3);
System.out.println();
System.out.println("Join multiple string via Java 8, StringJoiner:");
String stringV4 = Strings.joinByDelimiterV3(' ', TEXT_1, TEXT_2, TEXT_3, TEXT_4, TEXT_5);
System.out.println("String: " + stringV4);
}
}
Output:
Join multiple string via Java 8, String.join():
String: Illinois Mathematics and Science Academy
Join multiple string via StringBuilder:
String: Illinois Mathematics and Science Academy
Join multiple string via Java 8, Collectors.joining():
String: Illinois Mathematics and Science Academy
Join multiple string via Java 8, StringJoiner:
String: Illinois Mathematics and Science Academy
Comments
Post a Comment