Array to String in Java
There are many ways to convert Array to String in Java.
Arrays.toString() method
it is used to return a string representation of the contents of the specified array. The string consists of a list of the array's elements, enclosed in square brackets ("[]").
It returns null if the array is null.
package com.javacodestuffs.core.conversions;
import java.io.*;
import java.util.*;
public class ArraytoString {
public static void main(String[] args)
{
int[] intArray
= new int[] { 21, 31, 11, 51 };
Object[] objArray
= new Object[] { 21, 31, 11, 51 };
boolean[] boolArray
= new boolean[] { false, true, false, true };
char[] charArray
= new char[] { 'j', 'a', 'v', 'a', 'c', 'o','d','e','s','t','u','f','f','s'};
double[] doubleArray
= new double[] { 7, 5, 3, 1 };
float floatArray[] = new float[] {1f, 5f, 3f, 2f, 9f};
System.out.println(
"Character Array is : "
+ Arrays.toString(charArray));
System.out.println(
"Boolean Array is : "
+ Arrays.toString(boolArray));
System.out.println(
"Double Array is : "
+ Arrays.toString(doubleArray));
System.out.println(
"Integer Array is : "
+ Arrays.toString(intArray));
System.out.println(
"Object Array is : "
+ Arrays.toString(objArray));
}
}
output:
Character Array is : [j, a, v, a, c, o, d, e, s, t, u, f, f, s]
Boolean Array is : [false, true, false, true]
Double Array is : [7.0, 5.0, 3.0, 1.0]
Integer Array is : [21, 31, 11, 51]
Object Array is : [21, 31, 11, 51]
Using StringBuffer
Here we iterate over the array and the append the string to new string using sb.append method.
package com.javacodestuffs.core.conversions;
import java.util.Arrays;
public class ArrayToStrings0 {
public static void main(String args[]) {
String stringArray[] = {
"Array",
"to",
"String",
"in",
"Java",
"using",
"StringBuffer"
};
StringBuffer sb = new StringBuffer();
for (int i = 0; i < stringArray.length; i++) {
sb.append(stringArray[i]);
}
System.out.println("Using StringBuffer : ");
System.out.println();
System.out.println(sb);
}
}
output:
Using StringBuffer :
ArraytoStringinJavausingStringBuffer
Using Java Streams API
In java8, we have a String.join() method that produces a new string by joining elements and separating them with the specified delimiter.
Collectors.joining() method from the Java Streams API that joins strings from the Stream in the same order as its source.
package com.javacodestuffs.core.conversions;
import java.util. * ;
import java.util.stream. * ;
public class ArraytoString {
public static void main(String[] args) {
String joinedString1 = String.join("", new String[] {
"Array",
"to",
"String",
"in",
"Java",
"using",
"Streams"
});
String joinedString2 = Arrays.stream(new String[] {
"Array",
"to",
"String",
"in",
"Java",
"using",
"Streams"
}).collect(Collectors.joining());
System.out.println("Array to String using String.join() : " + joinedString1);
System.out.println("Array to String using Arrays.stream and Collectors.joining() : " + joinedString2);
}
}
output:
Array to String using String.join() : ArraytoStringinJavausingStreams
Array to String using Arrays.stream and Collectors.joining() : ArraytoStringinJavausingStreams
Using Apache Commons Lang's StringUtils.join()
We need the following dependency included to pom.xml in our maven project.
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.8.1</version>
</dependency>
StringUtilsJoin.java
package com.javacodestuffs.core.conversions;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import org.apache.commons.lang.StringUtils;
public class StringUtilsJoin {
public static void main(String[] args) throws IOException {
String[] stringArray = { "Welcome", "Hello", "World" };
System.out.println(StringUtils.join(stringArray));
System.out.println(StringUtils.join(stringArray, " "));
List<String> stringList = Arrays.asList(stringArray);
System.out.println(StringUtils.join(strList, null));
System.out.println(StringUtils.join(strList, " "));
String joinedString = StringUtils.join(new String[]{ "Array", "to", "String", "in","Java" });
System.out.println(joinedString);
}
}
output:
WelcomeHelloWorld
Welcome Hello World
WelcomeHelloWorld
Welcome Hello World
ArraytoStringinJava
Using Joiner.join() and Splitter from Google's Guava
We need Guava's dependency included to pom.xml in our maven project.
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>29.0-jre</version>
</dependency>
Using Splitter
package com.javacodestuffs.core.conversions;
import com.google.common.base.Splitter;
public class GuavaSplitter {
public static void main(String[] args) {
List<String> srtingList = Splitter.on(' ')
.trimResults()
.omitEmptyStrings()
.splitToList("Welcome to java programming");
String[] stringArray = srtingList.toArray(new String[0]);
System.out.println(stringArray);
}
}
output:
["Welcome","to","java","programming"]
Using Joiner
Joiner class offers API to convert array to String. Also, We can add a delimiter or skip null values.
package com.javacodestuffs.core.conversions;
import com.google.common.base.Splitter;
public class GuavaJoiner {
public static void main(String[] args) {
String joinedString = Joiner.on("")
.skipNulls()"Welcome to java programming"
.join(new String[]{ "Welcome", "to", "java","programming", null });
System.out.println(stringArray);
}
}
output:
Welcometojavaprogramming
In this article, we have seen how to Array to String in Java with examples. All source code in the article can be found in the GitHub repository.
0 Comments
Post a Comment