There are many scenarios when you want to join multiple strings to create a new string may be with a delimiter too between two strings (like | or ,). Earlier you would have used StringBuilder or StringBuffer class to append the string one by one for joining or may be you would have used any third party library like Apache Commons which has StringUtils class.
Code snippet using StringBuilder
StringBuilder sb = new StringBuilder();
boolean firstFlg = true;
String delimiter = “,”;
for (String str: strArray){
if (firstFlg){
firstFlg = false;
}
else{
sb.append(delimiter);
}
sb.append(str);
}
return
sb.toString();
join() method
Though String is a very important class and it provides a lots of methods for comparison of strings, searching with in string but somehow there was no method for joining multiple strings. With Java 8 join() method has been added in the String class which makes it very easy to join the multiple strings so let's see an example how to join strings in Java 8.
join() method in String class
join method has two overloaded versions -- public static String join(CharSequence delimiter, CharSequence... elements) - Returns a new String composed of copies of the CharSequence elements joined together with a copy of the specified delimiter.
- public static String join(CharSequence delimiter, Iterable<? extends CharSequence> elements) –Here elements is an Iterable that will have its elements joined together and delimiter is a sequence of characters that is used to separate each of the elements in the resulting String
Example code
- If you have 3 string variables which you want to join with space in between or with '-' in between then it can be done using the following Java code -
public class StringJoin {
public static void main(String[] args) {
String str1 = "An";
String str2 = "example";
String str3 = "string";
// joining with space
String finalStr = String.join("", str1, str2, str3);
System.out.println("str - " + finalStr);
// joining with hyphen
finalStr = String.join("-", str1, str2, str3);
System.out.println("str - " + finalStr);
}
}Output
str - An example string
str - An-example-string - If you have 3 string variables day, month and year which you want to join to create a date in format dd/mm/yyyy.
public class StringJoin {
public static void main(String[] args) {
String str1 = "26";
String str2 = "01";
String str3 = "2016";
String finalStr = String.join("/", str1, str2, str3);
System.out.println("str - " + finalStr);
}
}Output
str - 26/01/2016 - If you have list of Strings then you can use the second join method to join all the strings with in the list.
import java.util.ArrayList;
import java.util.List;
public class ListJoin {
public static void main(String[] args) {
List<String> strList = new ArrayList<String>();
strList.add("An");
strList.add("example");
strList.add("string");
// joining with comma as delimiter
String finalStr = String.join(",", strList);
System.out.println("str - " + finalStr);
}
}Output
str – An,example,string
StringJoiner Class
In Java 8 there is also a StringJoiner class that can be used for joining the Strings. It has two constructors.
- StringJoiner(CharSequence delimiter) - Constructs a StringJoiner with no characters in it, with no prefix or suffix, and a copy of the supplied delimiter.
- StringJoiner(CharSequence delimiter, CharSequence prefix, CharSequence suffix) - Constructs a StringJoiner with no characters in it using copies of the supplied prefix, delimiter and suffix.
Example code
Let us see an example to clarify things.
- If you have three strings which you want to join and delimiter is ':' then it can be done using StringJoiner class as follows -
import java.util.StringJoiner;
public class StringJoinerDemo {
public static void main(String[] args) {
String str1 = "An";
String str2 = "example";
String str3 = "string";
// providing delimiter
StringJoiner sj = new StringJoiner(":");
// adding strings that are to be joined
sj.add(str1).add(str2).add(str3);
System.out.println(sj.toString());
}
}Output
An:example:string - If you want to get the joined strings with suffix and prefix so that the end string looks like this - (An,example,string)
import java.util.StringJoiner;
public class StringJoinerDemo {
public static void main(String[] args) {
String str1 = "An";
String str2 = "example";
String str3 = "string";
// providing delimiter and suffix, prefix
StringJoiner sj = new StringJoiner(",", "(", ")");
// adding strings that are to be joined
sj.add(str1).add(str2).add(str3);
System.out.println(sj.toString());
}
}
That's all for this topic String join() method in Java 8. If you have any doubt or any suggestions to make please drop a comment. Thanks!
Related topics
You may also like -