Best way to convert an array of integers to a string in Java

17,763

Solution 1

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. EDIT: As pointed out below, you can use Ints.join for this.

Solution 2

   int[] x = new int[] {3,4,5};
   String s = java.util.Arrays.toString(x).replaceAll("[\\,\\[\\]\\ ]", "")

Update

For completeness the Java 8 Streams solution, but it isn't pretty (libraries like vavr would be shorter and faster):

String s = IntStream.of(x)
  .mapToObj(Integer::toString)
  .collect(Collectors.joining(""));

Solution 3

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.

Solution 4

StringBuffer str =new StringBuffer();
for(int i:x){  
str.append(i);
}  

You need to read all once at least.

Solution 5

public static void main(String[] args) {
    int[] dice = {1, 2, 3, 4, 5, 0};
    String string = "";

    for (int i = 0; i < dice.length; i++) {
        string = string + dice[i];
    }

    System.out.print(string);   

}

This is another way you can do it. Basically just makes a for-loop that accesses every element in the integer array and adds it to the string.

Share:
17,763
didxga
Author by

didxga

I am playing code for fun!

Updated on June 05, 2022

Comments

  • didxga
    didxga almost 2 years

    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"

  • Landei
    Landei over 13 years
    That yields "[3,4,5]", not "345"
  • Sachin Shanbhag
    Sachin Shanbhag over 13 years
    @Landei - yeah saw that.. Try now. Have edited the answer now
  • Jon Skeet
    Jon Skeet over 13 years
    That's going to give "3 4 5" now instead of "345".
  • Sachin Shanbhag
    Sachin Shanbhag over 13 years
    @Jon Skeet - Yep. Sorry, replace method is adding a space. Edited again. Thanks.
  • Sachin Shanbhag
    Sachin Shanbhag over 13 years
    @didxga - yes, its surely tricky and within two lines of code. Admit I got it from this link if you need more info - daniweb.com/forums/thread14183.html
  • Jon Skeet
    Jon Skeet over 13 years
    Personally I'd rather use my five lines of simple code than two lines of code which have already proved themselves relatively hard to get right :)
  • didxga
    didxga over 13 years
    Wow, I am lucky, I found that your answer to my question is just your 10000th answer in Stackoverflow,am i right? Congrats!
  • Ashish Patil
    Ashish Patil over 13 years
    I have up-voted your answer. But just a curious? What is more costly (time consuming)? StringBuilder.append(int) or y = y*10 + x; ? I know the primitives will work only till the Integer.MAX_VALUE, but if we always have less than that result, then what would be better?
  • Etienne Neveu
    Etienne Neveu about 13 years
    Guava's Joiner won't work for int primitives (it works with Objects), but Ints.join("", intArray) will: guava-libraries.googlecode.com/svn/tags/release09/javadoc/co‌​m/…
  • Code
    Code almost 11 years
    I have made use of list here
  • Mat
    Mat over 9 years
    Please don't (badly) copy-paste other answers into your own. (stackoverflow.com/a/16811343/635608)