To remove garbage characters from a string using regex...
Posted
by Harjit Singh
on Stack Overflow
See other posts from Stack Overflow
or by Harjit Singh
Published on 2010-05-31T11:44:29Z
Indexed on
2010/05/31
12:02 UTC
Read the original article
Hit count: 257
regex
Hi
I want to remove characters from a string other then a-z, and A-Z. Created following function for the same and it works fine.
public String stripGarbage(String s) {
String good = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
String result = "";
for (int i = 0; i < s.length(); i++) {
if (good.indexOf(s.charAt(i)) >= 0) {
result += s.charAt(i);
}
}
return result;
}
Can anyone tell me a better way to achieve the same. Probably regex may be better option.
Regards
Harry
© Stack Overflow or respective owner