is there a way in vb.net that i can run an if
statement to say if a variable starts with 07
then between 1-9
i know i could do this using substring
which would work but it would make the if statement rather large
number_called.Substring(0, 3) = "071" or number_called.Substring(0, 3) = "072"
and so on up to 079
but can i create a shorter if
statement for the whole range?
This would do it
Private Function CheckNumber(myNumber As String) As Boolean
Dim regex As Regex = New Regex("^07[1-9]]")
Dim match As Match = regex.Match(myNumber)
Return match.Success
End Function
Just call CheckNumber("071")
or CheckNumber(number_called)
Remember to import the references Imports System.Text.RegularExpressions
Updated Expression, thank you Veeke
If you know that it will always start with 3 numbers you can parse it
Dim num = Int32.Parse(number_called.Substring(0, 3))
Dim Valid= num>69 and num<80
If you dont know if it will start with 3 numbers, surround it with a TryCatch
You could use String.StartsWith("07")
and check the last character of the String
- it must be a number and not 0
like this:
If str.Length = 3 And str.StartsWith("07") And Char.IsNumber(str(2)) And str(2) <> "0" Then
End If
Small correction on Malcor's post, which doesn't check if it begins with '07' (just if it contains '07'):
Private Function CheckNumber(myNumber As String) As Boolean
Dim regex As Regex = New Regex("^07[1-9]")
Dim match As Match = regex.Match(myNumber)
Return match.Success
End Function
You could use StartsWith coupled with a Select Case if you are sure it is always a numeric string:
If number_called.StartsWith("07") Then
Select Case CInt(number_called.SubString(2, 1))
Case 1 to 9
'it matched
Case Else
'it didn't match
End Select
End If
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.