hello i have a string ch and i want to replace the special characters if it exist by "_" how can detect all special characters is there another way . can we use replaceAll and include all the special characters
for(int i=0; i<s.length; i ) {
var char = s[i];
if (char = "/" || char ="." || char ="$")
{
s[i] ="_"
}
}
CodePudding user response:
You can do it like this with regex
_string.replaceAll(RegExp('[^A-Za-z0-9]'), '_');
This will replace all characters except alphabets and numbers with _
CodePudding user response:
The best way to do this would be to use RegEx inside a .replaceAll() method. In your case you can use a regex that matches all character other than English Alphabets..
String result = s.replaceAll(RegExp('[^A-Za-z]'), '_');
Or you can be more specific and create a regex of your requirement