Skip to content

Convert String array to String in Java example

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.

Output

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.

Output

If you want to remove square brackets from the string, use the following regular expression to replace all square brackets with the empty string.

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.

Output

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.

Output

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.

Output

This example is a part of the Java String tutorial with examples.

Please let me know your views in the comments section below.

About the author

Leave a Reply

Your email address will not be published.