0

I have 10 lines of array that are first name space last name space zip code. All the zip codes start with different numbers. Is there a way to replace the #1 in the indexof below so that it searches for any number character instead?

    'open file
    inFile = IO.File.OpenText("Names.txt")

    'process the loop instruct until end of file
    intSubscript = 0
    Do Until inFile.Peek = -1 OrElse intSubscript = strLine.Length

        strLine(intSubscript) = inFile.ReadLine
        intSubscript = intSubscript + 1
    Loop

    inFile.Close()

    intSubscript = 0
    strFound = "N"

    Do Until strFound = "Y" OrElse intSubscript = strLine.Length
        intIndex = strLine(intSubscript).IndexOf("1")
        strName = strLine(intSubscript).Substring(0, intIndex - 1)
        If strName = strFullname Then
            strFound = "Y"
            strZip = strLine(intSubscript).Substring(strLine(intSubscript).Length - 5, 5)
            txtZip.Text = strZip
        End If
    Loop

End Sub
1
  • 1
    You should use a Boolean instead of strFound. Commented May 1, 2011 at 16:16

1 Answer 1

1

use a regular expression.

Regular expressions allow you to do pattern matching on text. It's like String.IndexOf() with wildcard support.

For example, suppose your source data looks like this:

 James Harvey  10939
 Madison  Whittaker 33893
 George Keitel  22982

...and so on.

Expressed in English, the pattern each line follows is this:

the beginning of the string, followed by
a sequence of 1 or more alphabetic characters, followed by 
a sequence of one or more spaces, followed by 
a sequence of 1 or more alphabetic characters, followed by 
a sequence of one or more spaces, followed by 
a sequence of 5 numeric digits, followed by 
the end of the string

You can express that very precisely and succintly in regex this way:

^([A-Za-z]+) +([A-Za-z]+) +([0-9]{5})$

Apply it in VB this way:

    Dim sourcedata As String = _
        "James Harvey  10939" & _
        vbcrlf & _
        "Madison  Whittaker 33893" & _
        vbcrlf & _
        "George Keitel  22982"

    Dim regex = "^([A-Za-z]+) +([A-Za-z]+) +([0-9]{5})$"

    Dim re = New Regex(regex)

    Dim lineData As String() = sourceData.Split(vbcrlf.ToCharArray(), _
                                                StringSplitOptions.RemoveEmptyEntries )

    For i As Integer = 0 To lineData.Length -1
        System.Console.WriteLine("'{0}'", lineData(i))
        Dim matchResult As Match = re.Match(lineData(i))
        System.Console.WriteLine("  zip: {0}", matchResult.Groups(3).ToString())
    Next i

To get that code to compile, you must import the System.Text.RegularExpressions namespace at the top of your VB module, to get the Regex and Match types.

If your input data follows a different pattern, then you will need to adjust your regex. For example if it could be "Chris McElvoy III 29828", then you need to adjust the regex accordingly, to handle the name suffix.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.