开发者

How to strip a string of all alpha's?

开发者 https://www.devze.com 2023-03-08 23:55 出处:网络
Dim phoneNumber As String = \"077 47578 587(num)\" How do i strip the above string off every character which isnt a 开发者_如何学Cnumber. So only the numbers are left and then check to make sure it
Dim phoneNumber As String = "077 47578 587(num)"

How do i strip the above string off every character which isnt a 开发者_如何学Cnumber. So only the numbers are left and then check to make sure it is 11 characters long?


dim number as string = Regex.Replace(phoneNumber,"[^0-9]","")

if number.length = 11 then
 'valid number
else
 'not valid
end if


  • You could loop on each character and check if it is a digit. While looping, check that the number of accepted characters (digits) is less than 11. or
  • use a regex to remove all the alpha but you still will have to count at the end ....


Dim phoneNumber As String = "077 47578 587(num)"

Dim newPhoneNumber = String.Empty 
For i = 0 To phoneNumber.Length - 1
    If IsNumeric(phoneNumber(i)) Then
        newPhoneNumber += phoneNumber(i)
    End If 
Next

Dim valid = newPhoneNumber.Length = 11


One possible solution is to treat the string as a character array, then retrieve only those characters with ascii codes within the paramaeters you define.

Ascii codes can be found at a resource such as: http://www.bolen.net/html/misc/ASCII-codes.html

Alternatively, you could use a regular expression to retrieve only the characters you want. My regex isn't so hot, so I can't give an example :)

0

精彩评论

暂无评论...
验证码 换一张
取 消