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,

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
String[] arr = { "Abc", "Def", "Ghi", "Jkl" };
String joined = String.join("", arr));
String[] arr = { "Abc", "Def", "Ghi", "Jkl" }; String joined = String.join("", arr));
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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
String[] arr = { "Abc", "Def", "Ghi", "Jkl"};
StringJoiner joiner = new StringJoiner("");
for (String string : arr) {
joiner.add(string);
}
System.out.println(joiner);
String[] arr = { "Abc", "Def", "Ghi", "Jkl"}; StringJoiner joiner = new StringJoiner(""); for (String string : arr) { joiner.add(string); } System.out.println(joiner);
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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
String[] arr = {"Abc", "Def","Ghi", "Jkl"};
String join = Arrays.
stream(arr).
collect(Collectors.joining());
String[] arr = {"Abc", "Def","Ghi", "Jkl"}; String join = Arrays. stream(arr). collect(Collectors.joining());
String[] arr = {"Abc", "Def","Ghi", "Jkl"};
String join = Arrays.
              stream(arr).
              collect(Collectors.joining());
  1. To get the stream over array elements, stream() method is used.
  2. collect() method is a terminal operation used to combine stream elements, with the help of a collector.
  3. 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.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Arrays.
stream(arr).
filter(s -> s != null).
collect(Collectors.joining());
Arrays. stream(arr). filter(s -> s != null). collect(Collectors.joining());
Arrays.
stream(arr).
filter(s -> s != null).
collect(Collectors.joining());

[the_ad id=”656″] 4. Guava library

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
String[] arr = {"Abc", "Def","Ghi", "Jkl"};
String join = Joiner.on("").join(arr);
System.out.print(join);
String[] arr = {"Abc", "Def","Ghi", "Jkl"}; String join = Joiner.on("").join(arr); System.out.print(join);
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.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
String[] arr = {"Abc", "Def","Ghi", "Jkl"};
String join = Joiner.on("").skipNulls().join(arr);
String[] arr = {"Abc", "Def","Ghi", "Jkl"}; String join = Joiner.on("").skipNulls().join(arr);
String[] arr = {"Abc", "Def","Ghi", "Jkl"};
String join = Joiner.on("").skipNulls().join(arr);

Add below dependency for this library

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
// 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'
// 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'
// 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'

5. Apache Commons Lang

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
String join = StringUtils.join(arr);
System.out.println(join);
String join = StringUtils.join(arr); System.out.println(join);
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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
// 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'
// 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'
// 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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
StringBuffer buffer = new StringBuffer();
for (String string : arr) {
buffer.append(string);
}
StringBuffer buffer = new StringBuffer(); for (String string : arr) { buffer.append(string); }
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.

Categorized in:

Java String,