How to remove special characters in the string except "- _". Now I use:
replaceAll("[^\\w\\s]", "")
it remove all special character but i want to keep "- _" . Can anyone tell me how should I do?
Use replaceAll("[^\\w\\s\\-_]", "");
What I did was add the underscore and hyphen to the regular expression. I added a \\ before the hyphen because it also serves for specifying ranges: a-z means all letters between a and z. Escaping it with \\ makes sure it is treated as an hyphen.
This might help:
replaceAll("[^a-zA-Z0-9_-]", "");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With