Java examples to join string array to produce single String. This code can be used to convert array to string in Java. We may need this information many times during development specially while parsing contents out of JSON or XML.
1. Join String Array – Java 8 String.join()
String.join()
method has two overloaded forms.
Join multiple string arguments
This method takes all strings in var-args format and all strings are passed as argument in the method. The return string is received by appending all strings delimited by argument separator.
String join(CharSequence delimiter, CharSequence... elements)
This method can be used to join multiple strings which are not yet in form of collection or array.
String joinedString = String.join(", ", "How", "To", "Do", "In", "Java"); System.out.println(joinedString); Output: How, To, Do, In, Java
Join array or list of strings
String join(CharSequence delimiter, Iterable<? extends CharSequence> elements)
This method is used to join array of strings or list of strings.
Java program to join list of strings
List<String> strList = Arrays.asList("How", "To", "Do", "In", "Java"); String joinedString = String.join(", ", strList); System.out.println(joinedString); Output: How, To, Do, In, Java
Java program to join array of strings
String[] strArray = { "How", "To", "Do", "In", "Java" }; String joinedString = String.join(", ", strArray); System.out.println(joinedString); Output: How, To, Do, In, Java
2. Java 8 StringJoiner for formatted output
Using StringJoiner
class, we can produce formatted output of joined string. This is specially useful while using lambda collectors.
2.1. Method Syntax
It’s constructor takes three arguments – delimiter
[mandatory], and optionally prefix
and suffix
.
StringJoiner(CharSequence delimiter) StringJoiner(CharSequence delimiter, CharSequence prefix, CharSequence suffix)
2.2. StringJoiner Example
Run the example with similar input as above example to join multiple strings. We want to format the output as [How, To, Do, In, Java]
, then we can use below code:
StringJoiner joiner = new StringJoiner(", ", "[", "]"); joiner.add("How") .add("To") .add("Do") .add("In") .add("Java"); Output: [How, To, Do, In, Java]
3. String list of string with Collectors.joining()
While using Java 8 lambda, we can use Collectors.joining() to convert list to String.
List<String> numbers = Arrays.asList("How", "To", "Do", "In", "Java"); String joinedString = numbers .stream() .collect(Collectors.joining(", ","[","]")); System.out.println(joinedString); Output: [How, To, Do, In, Java]
4. Join String Array using StringUtils.join()
The StringUtils class of the Commons Langs library has several join()
methods that can be used to combine an array or list of strings into a single string.
4.1. Maven Dependency
<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.7</version> </dependency>
4.2. StringUtils.join() Example
See given examples. First we are joining string array with empty string. In second example, we are joining array with comma.
String[] strArray = { "How", "To", "Do", "In", "Java" }; String joinedString = StringUtils.join(strArray); System.out.println(joinedString); String joinedString2 = StringUtils.join(strArray, ", "); System.out.println(joinedString2); Output: HowToDoInJava How, To, Do, In, Java
Use above given examples to concatenate string array with in Java.
Happy Learning !!
Reference(s):
Java 8 String Doc
StringJoiner Java Doc
Apache Commons StringUtils Doc
Nicolas
Hello! I saw in the example 2 the following:
String joinedString = numbers
.stream()
.map(i -> i.toString())…..
In this case I think that the map(..) is unnecessary because the collection is already a string.
Lokesh Gupta
Bang !! You are right. I will update the example.