String msg="what is up?";
String[] s = msg.split(" ");
String[] r = {"?","!",",",".","+"};
for(int i=0;i<s.length;i++){
for(int j=0;j<r.length;j++){
if(s.contains(r[j])){
s[i]=s[i].replaceAll(r[j], "");
}
}
}
Throws: Exception in thread "main" java.util.regex.PatternSyntaxException: Dangling meta character '?' near index 0 @ the replaceAll
Some googling showed that adding "\\" in front of the ? should fix it. But this doesn't seem to work. (IE r = {"\\?","!",",",".","+"}; )
It doesn't see that "\\?" is contained.
So I removed the check for containing...
String msg="what is up?";
String[] s = msg.split(" ");
String[] r = {"\\?","!",",",".","\\+"};
for(int i=0;i<s.length;i++){
for(int j=0;j<r.length;j++){
s=s[i].replaceAll(r[j], "");
}
System.out.println(s[i]);
}
It prints out nothing.
So some how, s[i] is empty after it replaces everything. I don't understand what's happening.
How can i remove these characters from my String?