VB.NET how can I check if String Contains alphabet characters and .?
The logic is a little off in the highest rated answer. The function will only validate the first character and then return true. Here's the tweak that would validate the entire string for alpha characters:
'Validates a string of alpha characters
Function CheckForAlphaCharacters(ByVal StringToCheck As String)
For i = 0 To StringToCheck.Length - 1
If Not Char.IsLetter(StringToCheck.Chars(i)) Then
Return False
End If
Next
Return True 'Return true if all elements are characters
End Function
Use this function:
Function CheckForAlphaCharacters(ByVal StringToCheck As String)
For i = 0 To StringToCheck.Length - 1
If Char.IsLetter(StringToCheck.Chars(i)) Then
Return True
End If
Next
Return False
End Function
Usage
Dim Mystring As String = "abc123"
If CheckForAlphaCharacters(Mystring) Then
'do stuff here if it contains letters
Else
'do stuff here if it doesn't contain letters
End If
I believe I have another way you could check if there was a letter in a string, if thats all you wanted to check. say you have a random string of characters. example "12312H231" the string isnt the same as this because it has a letter in it "12312h231" if you use string.tolower = string.toupper. it will be false if it has a letter, and true if it has numbers only.
if string1.toupper = string1.tolower then
'string1 is a number
else
'string1 contains a letter
end if