Java – Join Two or More Strings with Delimiter

To join given strings str1, str2,.. with a delimiter string delimiter, use String.join() method. Call String.join() method and pass the delimiter string delimiter followed by the strings str1, str2,.. we would like to join.

String.join() returns a single string with all the strings joined with delimiter in between them.

Java Program

public class Example {
	public static void main(String[] args) {
		String delimiter = "--";
		String str1 = "hello";
		String str2 = "world";
		String str3 = "welcome";
		String result = String.join(delimiter, str1, str2, str3);
		System.out.println("Resulting String:   " + result);
	}
}

Output

Resulting String:   hello--world--welcome

Conclusion

In this Java Tutorial, we learned how to join two or more strings with a specified delimiter in Java.