Search code examples
androidspecial-charactersremoveall

Reomve special character from Arraylist


I am trying to remove special character from arraylist. Not getting click how to do this?

I have 3 editfield and filling text after certain conditions means when 1 is filled then another can be filled. now when i click to save this. this returns an array like [hello,abc,zbz] for fields

private List<String> hashtagData;
hashtagData = new ArrayList<String>();

String status_message = status.getText().toString();
String status_message2 = status2.getText().toString();
String status_message3 = status3.getText().toString();
hashtagData.add(status_message);
hashtagData.add(status_message2);
hashtagData.add(status_message3);

But I am trying to remove "[]". Thank you if anybody can help.


Solution

  • Here try this:

    ArrayList<String> strCol = new ArrayList<String>();
        strCol.add("[a,b,c,d,e]");
        strCol.add(".a.a.b");
        strCol.add("1,2,].3]");
        for (String string : strCol) {
            System.out.println(removeCharacter(string));
        }   
    
    
    private String removeCharacter(String word) {
        String[] specialCharacters = { "[", "}" ,"]",",","."};
        StringBuilder sb = new StringBuilder(word);
        for (int i = 0;i < sb.toString().length() - 1;i++){
            for (String specialChar : specialCharacters) {
                if (sb.toString().contains(specialChar)) {
                    int index = sb.indexOf(specialChar);
                    sb.deleteCharAt(index);
                }
            }
        }
        return sb.toString();
    }