Print array without brackets and commas

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 my Android layout.

How do I print an array without the brackets and commas? The array contains slashes and gets replaced one-by-one when the correct letter is guessed.

I am using the usual .toString() function of the ArrayList class and my output is formatted like: [ a, n, d, r, o, i, d ]. I want it to simply print out the array as a single String.

I fill the array using this bit of code:

List<String> publicArray = new ArrayList<>();

for (int i = 0; i < secretWordLength; i++) {
    hiddenArray.add(secretWord.substring(i, i + 1));

And I print it like this:

TextView currentWordView = (TextView) findViewById(;

Any help would be appreciated.

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

Replace the brackets and commas with empty space.

String formattedString = myArrayList.toString()
    .replace(",", "")  //remove the commas
    .replace("[", "")  //remove the right bracket
    .replace("]", "")  //remove the left bracket
    .trim();           //remove trailing spaces from partially initialized arrays

Solution 2

Basically, don’t use ArrayList.toString() – build the string up for yourself. For example:

StringBuilder builder = new StringBuilder();
for (String value : publicArray) {
String text = builder.toString();

(Personally I wouldn’t call the variable publicArray when it’s not actually an array, by the way.)

Solution 3

You can use join method from android.text.TextUtils class like:


Solution 4


StringUtils.join(array, "");


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


probably the best one: Arrays.toString(arr)

Solution 5

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("", -> new String[n]));
String joinedNumbers2 = String.join("",;

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

Solution 6

If you use Java8 or above, you can use with stream() with native.
        .collect(Collectors.joining(" "));


Solution 7

the most simple solution for removing the brackets is,

1.convert the arraylist into string with .toString() method.

2.use String.substring(1,strLen-1).(where strLen is the length of string after conversion from arraylist).

3.Hurraaah..the result string is your string with removed brackets.

hope this is useful…:-)

Solution 8

I have used
Arrays.toString(array_name).replace(“[“,””).replace(“]”,””).replace(“, “,””);
as I have seen it from some of the comments above, but also i added an additional space character after the comma (the part .replace(“, “,””)), because while I was printing out each value in a new line, there was still the space character shifting the words. It solved my problem.

Solution 9

I used join() function like:

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

Which Prints:

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

Solution 10

String[] students = {"John", "Kelly", "Leah"};

System.out.println(Arrays.toString(students).replace("[", "").replace("]", " "));

//output: John, Kelly, Leah

Solution 11

You can use the reduce method provided for streams for Java 8 and above.Note you would have to map to string first to allow for concatenation inside of reduce operator., b) -> a + " " + b).get();

Solution 12

Just initialize a String object with your array

String s=new String(array);

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply