How do I find out if the first character of a string is a number in VB.NET?
19,028
Solution 1
Here's a scratch program that gives you the answer, essentially the "IsNumeric" function:
Sub Main()
Dim sValue As String = "1Abc"
Dim sValueAsArray = sValue.ToCharArray()
If IsNumeric(sValueAsArray(0)) Then
Console.WriteLine("First character is numeric")
Else
Console.WriteLine("First character is not numeric")
End If
Console.ReadLine()
End Sub
Solution 2
Public Function StartsWithDigit(ByVal s As String) As Boolean
Return (Not String.IsNullOrEmpty(s)) AndAlso Char.IsDigit(s(0))
End Function
Solution 3
Public Function StartsWithDigit(ByVal s As String) As Boolean
Return s Like "#*"
End Function
Comments
-
LiamGu almost 2 years
How do I check to see if the first character of a string is a number in VB.NET?
I know that the Java way of doing it is:
char c = string.charAt(0); isDigit = (c >= '0' && c <= '9');
But I'm unsure as to how to go about it for VB.NET.
Thanks in advance for any help.
-
Chris Dunaway over 14 yearsThe call to ToCharArray is not necessary. The first character of a string can be referenced by sValue(0).
-
schlebe about 2 yearsit is very tricky but it works. Usable pattern are defined here docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/…