I have a phone number like (123) 456-7890. I am using the replaceAll method to remove () and - and spaces from the string. I tried following
String phNo= "(123) 456-7890".replaceAll("[()-\\s]").trim();
but it's not working. Any solution?
This should work:
String phNo = "(123) 456-7890".replaceAll("[()\\s-]+", "");
In your regex:
\s should be \\s
\\-
+ as in [()\\s-]+ to increase efficiency by minimizing # of replacementsIf you are using Kotlin than
mobileNo.replace(Regex("[()\\-\\s]"), "")
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