dev_android
dev_android

Reputation: 493

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.

Upvotes: 0

Views: 2041

Answers (3)

Senguttuvan Amaladass
Senguttuvan Amaladass

Reputation: 161

can use below function to remove special character from string using regular expressions.

using System.Text;
using System.Text.RegularExpressions;

public string RemoveSpecialCharacters(string str)
{
return Regex.Replace(str, "[^a-zA-Z0-9_.]+", "", RegexOptions.Compiled);
}

Upvotes: 0

bean_droid
bean_droid

Reputation: 117

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();
}

Upvotes: 1

Badrul
Badrul

Reputation: 1642

Create regex which matches with your criteria, then loop through your list.

String myRegex = "[^a-zA-Z0-9]";
int index = 0;
for (String your_string : list)
list.set(index++, s.replaceAll(myRegex, ""));

Upvotes: 1

Related Questions