0

what I want to do is I want to remove "[" "]" from a string for this I tried to Iterate over characters of a String make an ArrayList of Characters and then convert the ArrayList back to a string but know I don't know how to convert the ArrayList of characters into a string

String text="Hell[o Worl]d!"
ArrayList<Character> filtredChars = new ArrayList<Character>();
                CharacterIterator it = new StringCharacterIterator(text);
                while(it.current() != CharacterIterator.DONE)
                {
                    if (it.current() != '[' && it.current() != ']')
                        filtredChars.add(it.current());
                    it.next();
                }
                

is it possible or is there another solution ?

Louay GOURRIDA
  • 190
  • 1
  • 3
  • 14

1 Answers1

1

I suggest you use a StringBuilder. Iterate the characters with a simple for loop, and append all that aren't [ or ]. Like,

String text = "Hell[o Worl]d!";
StringBuilder sb = new StringBuilder();
for (int i = 0; i < text.length(); i++) {
    char ch = text.charAt(i);
    if (ch != '[' && ch != ']') {
        sb.append(ch);
    }
}
text = sb.toString();
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249