Print array without brackets and commas

JavaAndroidArraysListCollections

Java Problem Overview


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));
    publicArray.add("-");
}

And I print it like this:

TextView currentWordView = (TextView) findViewById(R.id.CurrentWord);
currentWordView.setText(publicArray.toString());

Any help would be appreciated.

Java Solutions


Solution 1 - Java

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 - Java

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

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

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

Solution 3 - Java

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

TextUtils.join("",array);

Solution 4 - Java

first

StringUtils.join(array, "");

second

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

EDIT

probably the best one: Arrays.toString(arr)

Solution 5 - Java

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.

Solution 6 - Java

If you use Java8 or above, you can use with stream() with native.

publicArray.stream()
		.map(Object::toString)
		.collect(Collectors.joining(" "));

References

Solution 7 - Java

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 - Java

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 - Java

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

Solution 10 - Java

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

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

//output: John, Kelly, Leah

Solution 11 - Java

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.

publicArray.stream().map(String::valueOf).reduce((a, b) -> a + " " + b).get();

Solution 12 - Java

Just initialize a String object with your array

String s=new String(array);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSQDKView Question on Stackoverflow
Solution 1 - Javauser489041View Answer on Stackoverflow
Solution 2 - JavaJon SkeetView Answer on Stackoverflow
Solution 3 - JavaicastellView Answer on Stackoverflow
Solution 4 - JavaAlexRView Answer on Stackoverflow
Solution 5 - Javaernest_kView Answer on Stackoverflow
Solution 6 - JavaNamoView Answer on Stackoverflow
Solution 7 - JavaDavidView Answer on Stackoverflow
Solution 8 - JavaMarko PanushkovskiView Answer on Stackoverflow
Solution 9 - JavajazkatView Answer on Stackoverflow
Solution 10 - Javaim_grownishView Answer on Stackoverflow
Solution 11 - JavadouglasView Answer on Stackoverflow
Solution 12 - JavaAyush bhanuView Answer on Stackoverflow