Concatenating elements in an array to a string

前端 未结 19 2268
慢半拍i
慢半拍i 2020-12-08 02:43

I\'m confused a bit. I couldn\'t find the answer anywhere ;(

I\'ve got an String array:

String[] arr = [\"1\", \"2\", \"3\"];

then

相关标签:
19条回答
  • 2020-12-08 03:11

    Arrays.toString is formatting the output (added the brackets and commas). you should implement your own method of toString.

    public String toString(String[] arr){
        String result = "";
        for(String s : arr)
            result+=s;
        return result;
    }
    

    [edit] Stringbuilder is better though. see above.

    0 讨论(0)
  • 2020-12-08 03:12

    Using Guava's Joiner (which in r18 internally uses StringBuilder)

    String[]  arr= {"1","2","3"};
    Joiner noSpaceJoiner = Joiner.on("");
    noSpaceJoiner.join(arr))
    

    Joiner is useful because it is thread-safe and immutable and can be reused in many places without having to duplicate code. In my opinion it also is more readable.


    Using Java 8's Stream support, we can also reduce this down to a one-liner.

    String concat = Stream.of(arr).collect(Collectors.joining());
    

    Both outputs in this case are 123

    0 讨论(0)
  • 2020-12-08 03:13

    Use StringBuilder instead of StringBuffer, because it is faster than StringBuffer.

    Sample code

    String[] strArr = {"1", "2", "3"};
    StringBuilder strBuilder = new StringBuilder();
    for (int i = 0; i < strArr.length; i++) {
       strBuilder.append(strArr[i]);
    }
    String newString = strBuilder.toString();
    

    Here's why this is a better solution to using string concatenation: When you concatenate 2 strings, a new string object is created and character by character copy is performed.
    Effectively meaning that the code complexity would be the order of the squared of the size of your array!

    (1+2+3+ ... n which is the number of characters copied per iteration). StringBuilder would do the 'copying to a string' only once in this case reducing the complexity to O(n).

    0 讨论(0)
  • 2020-12-08 03:14

    Object class has toString method. All other classes extending it overrides the toString method so in for arrays also toString is overrided in this way. So if you want your output in your way you need to create your own method by which you will get your desired result.

    str="";
    for (Integer i:arr){
         str+=i.toString();
    }
    

    Hope this helps

    refer Why does the toString method in java not seem to work for an array

    0 讨论(0)
  • 2020-12-08 03:15

    For those who develop in Android, use TextUtils.

    String items = TextUtils.join("", arr);
    

    Assuming arr is of type String[] arr= {"1","2","3"};

    The output would be 123

    0 讨论(0)
  • 2020-12-08 03:15

    Arrays.toString() can be used to convert String Array to String. The extra characters can be removed by using Regex expressions or simply by using replace method.

    Arrays.toString(strArray).replace(",", "").replace("[", "").replace("]", "");
    

    Java 8 has introduced new method for String join public static String join(CharSequence delimiter, CharSequence... elements)

    String.join("", strArray);
    
    0 讨论(0)
提交回复
热议问题