Java Basic Examples RegEx

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. Example also shows how to convert int array to String using Arrays or StringBuffer/StringBuilder class.

How to convert int array to String in Java?

Use any of the below given approaches to convert int array to String.

1) Convert int array to String using Arrays class

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

Output

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

Output

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

Output

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

2) Convert int array to String using StringBuilder/StringBuffer class

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 StringBuilder object. Finally, we will convert StringBuilder object to String object using toString method.

Output

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

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

 

Join 1000+ fellow learners! Enter your email address below: