Check if string contains Letters?
If you need help with a project or need to know how to do something specific in VB.NET then please ask your questions in here.
Forum rules
Please LOCK your topics once you have found the solution to your question so we know you no longer require help with your query.
Please LOCK your topics once you have found the solution to your question so we know you no longer require help with your query.
5 posts
Page 1 of 1
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?
Nailing my feet to the floor is easier than using my own muscles to balance, but you don't see me doing that 

Code: Select all
You can replace s by the name of your string, and the messagebox is optionnal. 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!")
This uses ASCII character codes to determine whether or not the String contains something other than numbers (anything not between 48 and 57, basically).
you can also use the isNumeric function:
Code: Select all
if isNumeric(StringVariable) then
...
end if
function
Code: Select all
usageFunction 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
Code: Select all
##credits stackoverflow 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
Find my programs on Softpedia
Thanks, I spent like 30-40 minutes googling but stack overflow didn't popup.
Nailing my feet to the floor is easier than using my own muscles to balance, but you don't see me doing that 

5 posts
Page 1 of 1
Copyright Information
Copyright © Codenstuff.com 2020 - 2023