Convert int array to String in Java example

Convert int array to String in Java example shows how to convert int array to String in Java using the Arrays class or StringBuffer/StringBuilder class.

How to convert int array to String in Java?

1) Convert int array to String using the Arrays class

You can use the toString method of the Arrays class which accepts int array and returns a string representation of it as given below.

Output

All int values are wrapped in square brackets and are separated by a comma followed by a space. If you want to remove the square brackets from the string, use below given regular expression pattern along with the replaceAll method of the String class.

Output

If you want to remove space and comma as well, use below given regular expression pattern.

Output

You can also use the “[^0-9]” pattern to remove square brackets, spaces, and comma from the output. The “[^0-9]” pattern means “not digit”, so when it is used along with the replaceAll method, it replaces all the non-digit characters with an empty string, thus giving us only the int values.

2) Using StringBuilder or StringBuffer class

The StringBuilder class can be used to convert int array to string as given below. We will iterate through int array and append each element of an array to the StringBuilder object. Finally, we will convert the StringBuilder object to String object using the toString method.

Output

Note: If you are using Java 1.4 or below version, use the StringBuffer instead of the StringBuilder class.

This example is a part of the Java String tutorial.

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

About the author

RahimV

RahimV

My name is RahimV and I have over 16 years of experience in designing and developing Java applications. Over the years I have worked with many fortune 500 companies as an eCommerce Architect. My goal is to provide high quality but simple to understand Java tutorials and examples for free. If you like my website, follow me on Facebook and Twitter.

Add Comment

Your email address will not be published. Required fields are marked *