Convert string array to string in Java example shows how to convert an array of string to string in Java using various approaches.
How to convert string array to string in Java?
1) Using for loop and StringBuilder
You can use enhanced for loop to iterate over an array and append each element of an array to the StringBuilder object. Finally, you can convert the StringBuilder to a string using the toString
method as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
package com.javacodeexamples.basic.arrayexamples; public class StringArrayToStringExample { public static void main(String[] args) { String[] strArray = { "Java", "String", "Array", "to", "String", "Example" }; StringBuilder sbOutput = new StringBuilder(); for(String str : strArray){ sbOutput.append(str); sbOutput.append(" "); } //convert StringBuffer to String String strOutput = sbOutput.toString(); System.out.println(strOutput); } } |
Output
1 |
Java String Array to String Example |
Note: If you are using Java 1.4 or earlier version, use the StringBuffer instead of the StringBuilder class.
2) Using Arrays class
You can convert string array to List and then use toString
method of Arrays class to convert it to the string as given below.
1 2 3 4 5 6 7 |
//convert array to List first List<String> list = Arrays.asList(strArray); //use toString method to convert List to String String strOutput = list.toString(); System.out.println(strOutput); |
Output
1 |
[Java, String, Array, to, String, Example] |
If you want to remove square brackets from the string, use the following regular expression to replace all square brackets with the empty string.
1 |
strOutput = strOutput.replaceAll("\\[|\\]", ""); |
3) Using the Apache Commons library
If you are using the Apache Commons library, you can use the join
method of the StringUtils class to join array elements as given below.
1 2 3 4 5 6 7 8 |
/* * Use join method of StringUtils class. * You can also provide separator as a second * argument to the join method */ String strOutput = StringUtils.join(strArray, ", "); System.out.println(strOutput); |
Output
1 |
Java, String, Array, to, String, Example |
4) Using the join method of String class (Java 8)
If you are using Java 8 or later version, you can use the join
method of the String class to join array elements and create string as given below.
1 2 3 4 5 6 7 8 |
/* Use join method of String class to join * array elements. You can also provide separator * as a first argument to join method. */ String strOutput = String.join(", ", strArray); System.out.println(strOutput); |
Output
1 |
Java, String, Array, to, String, Example |
5) Using the toString method of the Arrays class
You can use the toString
method of Arrays class to convert array to string object as given below.
1 2 3 |
String strOutput = Arrays.toString(strArray); System.out.println(strOutput); |
Output
1 |
[Java, String, Array, to, String, Example] |
This example is a part of the Java String tutorial with examples.
Please let me know your views in the comments section below.