In Java, I have an array of integers. Is there a quick way to convert them to a string?
I.E. int[] x = new int[] {3,4,5}
x toString() should yield "345"
In Java, I have an array of integers. Is there a quick way to convert them to a string?
I.E. int[] x = new int[] {3,4,5}
x toString() should yield "345"
StringBuffer str =new StringBuffer();
for(int i:x){
str.append(i);
}
You need to read all once at least.
Simplest performant approach is probably StringBuilder:
StringBuilder builder = new StringBuilder();
for (int i : array) {
builder.append(i);
}
String text = builder.toString();
If you find yourself doing this in multiple places, you might want to look at Guava's Joiner
class - although I don't believe you'll be able to use it for primitive arrays.
Try with this - you have to import java.util.Arrays
and then -
String temp = Arrays.toString( intArray ).replace(", ", "");
String finalStr = temp.substring(1, temp.length()-2);
Where intArray is your integer array.
int[] x = new int[] {3,4,5};
String s = java.util.Arrays.toString(x).replaceAll("[\\,\\[\\]\\ ]", "")