In this article, we will take a look at following different ways to convert a string array to string in java
[the_ad id=”651″] 1. String.join()
Java string class has a join() method, which accepts two arguments
a) A separator
b) An array of CharSequence interface
and returns a string with elements of array combined with the separator.
Since string implements CharSequence, it can be supplied to join(). Example,
String[] arr = { "Abc", "Def", "Ghi", "Jkl" };
String joined = String.join("", arr)); This example joins string array elements without any separator. If you want, you can supply a separator as the first argument.
join() internally loops over the array and uses Java 8 StringJoiner class(discussed next) to convert array to string.
2. Using StringJoiner
Java 8 StringJoiner is used for joining string values using its add() method.
To convert a string array to string, it can be utilized as below
String[] arr = { "Abc", "Def", "Ghi", "Jkl"};
StringJoiner joiner = new StringJoiner("");
for (String string : arr) {
joiner.add(string);
}
System.out.println(joiner); Constructor of StringJoiner accepts a string, which acts as a delimiter pr separator between joined strings.
So, if you want to join strings with comma as a separator, then provide it as constructor argument.
3. Java streams
Java 8 streams can be used to convert a string array to string as shown below
String[] arr = {"Abc", "Def","Ghi", "Jkl"};
String join = Arrays.
stream(arr).
collect(Collectors.joining()); - To get the stream over array elements,
stream()method is used. collect()method is a terminal operation used to combine stream elements, with the help of a collector.- To get the collector,
Collectors.joining()method is used.
joining()joins the stream elements in to a string and returns a collector object.
This method will concatenate any null elements to the resultant string.
To avoid null elements, use stream filter() method as shown below.
Arrays. stream(arr). filter(s -> s != null). collect(Collectors.joining());
[the_ad id=”656″] 4. Guava library
String[] arr = {"Abc", "Def","Ghi", "Jkl"};
String join = Joiner.on("").join(arr);
System.out.print(join); If there is a null string in the array, then this method will raise a NullPointerException.
To avoid this, add a skipNulls() method before join(), so that it will ignore any null elements.
String[] arr = {"Abc", "Def","Ghi", "Jkl"};
String join = Joiner.on("").skipNulls().join(arr); Add below dependency for this library
// MAVEN
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>31.1-jre</version>
</dependency>
// GRADLE
implementation 'com.google.guava:guava:31.1-jre'
String join = StringUtils.join(arr); System.out.println(join);
join() automatically converts null to an empty string.
Following dependency needs to be added for this library
// MAVEN
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>
// GRADLE
implementation 'org.apache.commons:commons-lang3:3.12.0'
6. Using StringBuffer
Iterate over the array with a for loop and add elements to a StringBuffer with its append() method as shown below
StringBuffer buffer = new StringBuffer();
for (String string : arr) {
buffer.append(string);
} You can
[the_ad id=”644″] That is all on different ways to convert a string array to a string in java.