Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Read a text file 1

Status
Not open for further replies.

brews

Technical User
Dec 12, 2007
194
US
Reading a text file locating certain positions, I am using:
Code:
            Dim Lines = _
        ( _
            From line In IO.File.ReadAllLines(OpenFD.FileName) _
            Where line.Length > 0 AndAlso _
            Not line.StartsWith("5") AndAlso _
            Not line.StartsWith("9") _
         ).ToList     'excludes lines starting with 5 or 9
           'next certain positions are read

            For Each line In Lines
                txtAccountNumber.Text = line.Substring(21, 4)
                txtLastName.Text = line.Substring(72, 15)
                txtFirstName.Text = line.Substring(87, 15)
                txtStreetNumber.Text = line.Substring(102, 5)
                txtStreetNumber.Text = Replace(LTrim(Replace(txtStreetNumber.Text, "0", " ")), " ", "0")
                txtAddress.Text = line.Substring(107, 20)
                txtCity.Text = line.Substring(132, 15)
                txtState.Text = line.Substring(147, 2)
                txtZip.Text = line.Substring(149, 5)
                mskPhone.Text = line.Substring(154, 10)
             Next
              'the info is then saved.

The problem is reading txtStreetNumber.Text = line.Substring(102, 5). If the number is a four digit one such as 3226, 32260 is what is extracted. What needs to be done to correct?

Thank you.
 
Can you post a couple of example lines of text that you are extracting values from?
 
Not tested at all but try this and see what happens:

TextBox1.Text = line.ToString.PadLeft(5, "0").Substring(21, 4)
 
Thanks for the replies. This is a typical text record:
Code:
6009412201211121236265000                     000000000       0002864   BADA           KEMI           5300 W GULF BANK RD      203  HOUSTON        TX77088          00000000000000300          000000000                    10100430010000122000001EPC31700941201002845400

I used this TextBox1.Text = line.ToString.PadLeft(5, "0").Substring(21, 4)subbing "" for "0". Seems to me that if using the padding function and an extra 0 is entered, then a null is needed, no?
 
In the example provided in position 102, 5 characters long you have ' 5300'
Do you want back just '53'? That would be wrong, right?

Show us the 'problem' record where you claim you have 3226, but returns 32260

Have fun.

---- Andy
 
In your example, the street number would be 5300?

What about
Code:
txtStreetNumber.Text = line.Substring(102, 5).Trim


It appears you are dealing with fixed width fields. If you know the starting position and width of the address field, you can set up a regular expression to parse the address. This will have the advantage of properly catching one or two digit street numbers such as 5 INDUSTRIAL DR.

Code:
        'grab the address field (assuming it starts at column 102 and is 25 characters wide)
        Dim strAddress = strInput.Substring(102, 25)
        'group 1: (\d+), the street number
        'group 2: (.+), the rest of the street address
        Dim regAddress As Regex = New Regex("(\d+) (.+)")
        Dim m As Match

        m = regAddress.Match(strAddress)
        If m.Success Then
            txtStreetNumber.Text = m.Groups(1).Value
            txtAddress.Text = m.Groups(2).Value.Trim
        End If

The above code will need
Imports System.Text.RegularExpressions
or fully qualify the regular expression stuff.
 
either this record or the previous added a 0 under the original code. Therefore, it returned 53000 and 32260:
Code:
6000436201211121157005000                     000000000       0004529   BOBADILLO      JACQUELYN      3226 ANDRICKS RD              LA PORTE       TX77571          00000000000000300          000000000                    10100350010000121000001EPC31700043601004542974
jges- yes, street#=5300 in one example. I used the trim function as suggested, and it appears to work.

if the substring
Code:
txtStreetNumber.Text = line.Substring(102, 5).trim
works, please explain the purpose of the 'regular expression'. Does that mean there is a 'regular expression' needed for each field?

Thanks for the replies.

 
You would not need a regular expression for each field.

I suggested a regular expression because the current code chops off the first 5 characters and assumes it is the street number. What about an address like 1 Infinite Loop, Cupertino CA? For the street address, your code would return 1 Inf unless the street number has its own field (hard to tell from the example string). The regular expression would be a concise way to parse the address; not necessary depending on your field setup and knowledge of incoming data.
 
Thanks for the explanation. The street address field is 5 and either shows up as the number it is and the rest of the field is blank or filled with preceeding 0's. I'll go with the .trim function for testing.

thanks for your help
 
I better understand the situation now. I believe the problem stems from this line:
Code:
txtStreetNumber.Text = Replace(LTrim(Replace(txtStreetNumber.Text, "0", " ")), " ", "0")

This line replaces all zeros with spaces, then performs a left trim (removes leading spaces) and replaces all spaces with zeros. Following these steps, the input 03226 would be:
03226
_3226 (where the underscore represents a space)
3226
If your input is 3226_ the output will be 32260.

Is it possible this particular record was right padded instead of left padded? Another possibility is the column count is off by 1. Perhaps the code grabbed "3226 " instead of "03226" If you have done work with VBA or older versions of VB (6 or earlier), many string functions are 1 based. In .net, all string functions are zero based.
 
It appears you are simply trying to zero suppress the street number. Replace this
Code:
txtStreetNumber.Text = Replace(LTrim(Replace(txtStreetNumber.Text, "0", " ")), " ", "0")
with this
Code:
Dim streetNumber As Integer
If (Integer.TryParse(txtStreetNumber.Text, streetNumber)) Then txtStreetNumber.Text = streetNumber.ToString
 
Dave, you say zero suppress. Only if it is not part of the address. Found out that the proceeding zero was added when checking the second record whose address is 3226 and not 32260.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top