VB.NET function to check starting substring

Sam OZ
Sam OZ used Ask the Experts™
on
I am looking for VB.NET function to check if a string is starting with anything between 500 to 599
 

Example strings
   523 Doc1
   Doc1
   5002Doc2
   Document523SubDoc
   522-Doc3

The valid strings from the above are
   523 Doc1
   522-Doc3
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Software Team Lead
Commented:
try like:

Dim yourString() As String = {"523 Doc1", "Doc1", "5002Doc2", "Document523SubDoc", "522-Doc3"}
        Dim resultString() As String
        Dim cnt As Integer = 0
        Dim num As Integer

        For Each value As String In yourString
            Dim startNo As String = Regex.Match(value, "^\d+").Value
            Integer.TryParse(startNo, num)

            If num >= 500 And num <= 599 Then
                ReDim Preserve resultString(cnt)
                resultString(cnt) = value
                cnt = cnt + 1
            End If
        Next

        MsgBox(String.Join(vbCrLf, resultString))

Open in new window

Author

Commented:
Thanks . But I don't need this to come as an array . At one time only one string is taken ( For example Doc1).  The function just need to check the rule of 5xx and return true or false
I hope the StartNo variable is god enough
Ryan ChongSoftware Team Lead

Commented:
yup, similar concept applied for one string, hence use the part mentioned below should be good enough:

Dim startNo As String = Regex.Match(value, "^\d+").Value
            Integer.TryParse(startNo, num)

Open in new window


and then refer to the value of num

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial