Print array without brackets and commas

后端 未结 10 726
囚心锁ツ
囚心锁ツ 2020-11-29 23:28

I\'m porting a Hangman game to Android and have met a few problems. The original Java program used the console, so now I have to somehow beautify the output so that it fits

相关标签:
10条回答
  • 2020-11-30 00:26

    Just initialize a String object with your array

    String s=new String(array);
    
    0 讨论(0)
  • 2020-11-30 00:28

    With Java 8 or newer, you can use String.join, which provides the same functionality:

    Returns a new String composed of copies of the CharSequence elements joined together with a copy of the specified delimiter

    String[] array = new String[] { "a", "n", "d", "r", "o", "i", "d" };
    String joined = String.join("", array); //returns "android"
    

    With an array of a different type, one should convert it to a String array or to a char sequence Iterable:

    int[] numbers = { 1, 2, 3, 4, 5, 6, 7 };
    
    //both of the following return "1234567"
    String joinedNumbers = String.join("",
            Arrays.stream(numbers).mapToObj(String::valueOf).toArray(n -> new String[n]));
    String joinedNumbers2 = String.join("",
            Arrays.stream(numbers).mapToObj(String::valueOf).collect(Collectors.toList()));
    

    The first argument to String.join is the delimiter, and can be changed accordingly.

    0 讨论(0)
  • 2020-11-30 00:29

    first

    StringUtils.join(array, "");

    second

    Arrays.asList(arr).toString().substring(1).replaceFirst("]", "").replace(", ", "")

    EDIT

    probably the best one: Arrays.toString(arr)

    0 讨论(0)
  • 2020-11-30 00:29

    I used join() function like:

    i=new Array("Hi", "Hello", "Cheers", "Greetings");
    i=i.join("");
    

    Which Prints:
    HiHelloCheersGreetings


    See more: Javascript Join - Use Join to Make an Array into a String in Javascript

    0 讨论(0)
提交回复
热议问题