Quick add on requirement in our project. A field in our DB to hold a phone number is set to only allow 10 characters. So, if I get passed ‘(913)-444-5555’ or anything else, is there a quick way to run a string through some kind of special replace function that I can pass it a set of characters to allow?
Regex?
Definitely regex:
or within a class to avoid re-creating the regex all the time:
Depending on your real-world inputs, you may want some additional logic there to do things like strip out leading 1’s (for long distance) or anything trailing an x or X (for extensions).