Page 1 of 1

Check if string contains Letters?

Posted: Sun Jan 13, 2013 9:13 pm
by zachman61
I'm trying to check if a string contains letters because I need a numerical only string, anyone know how to check if it contains letters?

Re: Check if string contains Letters?

Posted: Sun Jan 13, 2013 9:41 pm
by comathi
Code: Select all
Dim containsLetter As Boolean
        For i = 0 To s.Length - 1
            If Asc(s.Substring(i, 1)) < 48 Or Asc(s.Substring(i, 1)) > 57 Then containsLetter = True
        Next
        If containsLetter Then MessageBox.Show("The string must contain numbers only!")
You can replace s by the name of your string, and the messagebox is optionnal.

This uses ASCII character codes to determine whether or not the String contains something other than numbers (anything not between 48 and 57, basically).

Re: Check if string contains Letters?

Posted: Mon Jan 14, 2013 1:14 am
by Cheatmasterbw
you can also use the isNumeric function:
Code: Select all
if isNumeric(StringVariable) then
...
end if

Re: Check if string contains Letters?

Posted: Mon Jan 14, 2013 2:26 am
by Shim
function
Code: Select all
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
Code: Select all
 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
##credits stackoverflow

Re: Check if string contains Letters?

Posted: Mon Jan 14, 2013 10:38 am
by zachman61
Thanks, I spent like 30-40 minutes googling but stack overflow didn't popup.