Andrew Chu
Andrew Chu

Reputation: 21

Convert array to string without brackets, commas, or spaces

I'm trying to convert an array list of individual letters (e.g. {"a", "b", "c"}) into a string that is that is made from all those letters combined — in order — to make a word. So with the example above, the array would be {"a", "b", "c"} and the word would be "abc". I'm currently using the code below:

ArrayList<String> arr = new ArrayList<String>();
String str = "a"; //this is just to define it before hand with a random value
*after a lot more code*
str = Arrays.toString(arr.toArray()).replace("[", "").replace("]", "").replace(",", "");
System.out.println(str);

This works — I get my result without brackets or commas, but since I replaced the commas and brackets with spaces, I get "a b c" instead of "abc". Is there a way to change this so I can replace the commas with purely nothing instead of spaces? I guess a better way to phrase this would be to completely remove the commas and brackets.

Thanks so much for any help.

Upvotes: 1

Views: 10730

Answers (5)

Eric mwaniki
Eric mwaniki

Reputation: 1

Adding replaceAll() worked for me.

str = Arrays.toString(arr.toArray()).replace("[", "").replace("]", "").replace(",", "").replaceAll(" ", "")

Upvotes: 0

Kang An
Kang An

Reputation: 41

String[] sa = new String[] {"a", "b", "c"};
System.out.println(String.join("", sa));

The Stream API is defined in JDK1.8

Upvotes: 4

Matt
Matt

Reputation: 3760

When you call:

System.out.println(stringOne);

What is really happening is System.out.println implicitly calls the toString() method of the array to obtain its string representation:

System.out.println(stringOne.toString());

The implementation of Array.toString() returns the array elements (or rather, the toString() representation of the array elements), comma separated.

If you would like a different representation you have two options. You can create a new array class that extends the base array class, and overrides the toString() method to return the desired string representation. This is .. probably not what you want to do, but it is an option.

Alternatively, you can process the array yourself and construct the desired string representation. @Elliott Frisch shows a nice way to do this using the Streams API available in Java 8.

There are many ways other ways you could achieve the same result. Here's a quick and dirty for loop implementation:

StringBuilder arrayAsString = new StringBuilder();
String delimiter = "";
for(String element : arr) {
    arrayAsString.append(delimiter).append(element);
    delimiter = ","; // I hate this idiom but it's handy for joining strings when you don't have a better .join() method available.
}

System.out.println(arrayAsString.toString()); // calling toString() explicitly for clarity, but it's not necessary

Upvotes: 1

alawand
alawand

Reputation: 46

Assuming you're looking to strip down anything from the input string that is not an alphabetic character, you can use this :

    StringBuilder sb = new StringBuilder(); 

    for (int i = 0; i < input.length(); i++) {
        if (Character.isAlphabetic(input.charAt(i))) {
            sb.append(input.charAt(i));
        }
    } 

    return sb.toString();

Upvotes: 3

Elliott Frisch
Elliott Frisch

Reputation: 201439

You could use a Stream and Collectors.joining() like

String[] arr = { "a", "b", "c" };
System.out.println(Stream.of(arr).collect(Collectors.joining()));

which outputs (as requested)

abc

Upvotes: 3

Related Questions