Dim phoneNumber As String = "077 47578 587(num)"
How do i strip the above string off every character which isnt a number. So only the numbers are left and then check to make sure it is 11 characters long?
Here in our tutorial, let us do this using isalnum(). We can also do this using regular expressions and isalpha(), isnumeric(). Most importantly, we use isalnum() because it is the easiest way to delete alphanumeric elements from the list.
The idea is to use String. replaceAll() method that replaces all the sequence of characters that matches the given Regular Expression with the given replacement string.
Remove all non alphanumeric characters from a string in C++The std::remove_if algorithm returns an iterator that indicates where the end should be, which can be passed to the std::erase function. Starting with C++20, consider using the std::erase_if function that is error-free wrapper over the erase-remove idiom.
Using isalnum() function Another option is to filter the string that matches with the isalnum() function. It returns true if all characters in the string are alphanumeric, false otherwise.
dim number as string = Regex.Replace(phoneNumber,"[^0-9]","")
if number.length = 11 then
'valid number
else
'not valid
end if
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