Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an isAlpha function in VB.NET

I have a variable to hold an id

Dim empId as String

So the format for a valid ID is:

'the first character should be a letter [A-Z]
'the rest of the string are digits e.g M2895 would be a valid id

I would like to check each of those characters to see if they fit the correct ID

So far, I have come across the isNumeric() function. Is there a similar function in VB.NET to check if a character is a string or alpha character?

like image 904
Redgren Grumbholdt Avatar asked Dec 24 '22 13:12

Redgren Grumbholdt


1 Answers

You can use RegularExpressions instead of checking each character of your string by hand:

Dim empId as String = "M2895"
If Regex.IsMatch(empId, "^[A-Z]{1}[0-9]+$") Then
    Console.WriteLine("Is valid ID")
End If

If you need a function isAlpha you can create this function by using RegularExpressions too:

Private Function isAlpha(ByVal letterChar As String) As Boolean
    Return Regex.IsMatch(letterChar, "^[A-Z]{1}$")
End Function

For completion, to support estonian alphabet too, you can use the following:

Dim empId as String = "Š2859"
If Regex.IsMatch(empId, "^[^\W\d_]{1}[0-9]+$") Then
    Console.WriteLine("Is valid ID")
End If
like image 104
Sebastian Brosch Avatar answered Jan 08 '23 06:01

Sebastian Brosch