issue with split function in vb.net

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • NewToThis

    #1

    issue with split function in vb.net

    I am trying to use the split function to bread up lines in a file I am
    reading from. Some lines are working just fine, but a couple of the lines
    don't split up the way I would have thought.

    Here's part of the code.
    Dim strDelim As String = "*~"
    Dim delimiter As Char() = strDelim.ToChar Array
    Dim split As String() = Nothing
    Dim fieldCount As Integer
    Dim s As String

    split = line.Split(deli miter, line.Split.GetU pperBound(0))
    fieldCount = 0
    For Each s In split
    Dim y As String = s
    If y <> "" Then
    fieldCount = fieldCount + 1
    End If
    Next s
    strAddress = split.GetValue( fieldCount - 1)

    Here's a sample line that works
    N3*SU*1685 N MAIN ST~
    Here's a line that doesn't work
    N3*ST*MAP*24000 Honda Pkwy~
    The line that doesn't work breaks after the N3, but it includes everything
    else in the second split.
    The line that works splits everything fine.
    Let me know what I can change, or what I am doing wrong.


    --
    Thanks
  • Kevin Yu [MSFT]

    #2
    RE: issue with split function in vb.net

    Hi,

    First of all, I would like to confirm my understanding of your issue. From
    your description, I understand that you are unable to split some of your
    string lines. If there is any misunderstandin g, please feel free to let me
    know.

    I think the part "line.Split.Get UpperBound(0)" is not necessary in the
    code, because the Split method will split according to all delimiters
    without specifying the count. So I changed the code to the following. It
    works fine on my machine.

    Dim strDelim As String = "*~"
    Dim delimiter As Char() = strDelim.ToChar Array
    Dim split As String() = Nothing
    Dim fieldCount As Integer
    Dim s As String

    split = line.Split(deli miter)
    fieldCount = 0
    For Each s In split
    Dim y As String = s
    If y <> "" Then
    fieldCount = fieldCount + 1
    End If
    Next s
    Dim strAddress As Object = split.GetValue( fieldCount - 1)

    HTH.

    Kevin Yu
    =======
    "This posting is provided "AS IS" with no warranties, and confers no
    rights."

    Comment

    • NewToThis

      #3
      RE: issue with split function in vb.net

      Yes you are correct. If you look at the 2 lines I gave as an example. The
      first one
      N3*SU*1685 N MAIN ST~
      splits at the * just fine, giving me 3 fields.
      The second one
      N3*ST*MAP*24000 Honda Pkwy~
      only splits after the N3 giving me only 2 fields and not the 4 I would expect.

      --
      Thanks


      "Kevin Yu [MSFT]" wrote:
      [color=blue]
      > Hi,
      >
      > First of all, I would like to confirm my understanding of your issue. From
      > your description, I understand that you are unable to split some of your
      > string lines. If there is any misunderstandin g, please feel free to let me
      > know.
      >
      > I think the part "line.Split.Get UpperBound(0)" is not necessary in the
      > code, because the Split method will split according to all delimiters
      > without specifying the count. So I changed the code to the following. It
      > works fine on my machine.
      >
      > Dim strDelim As String = "*~"
      > Dim delimiter As Char() = strDelim.ToChar Array
      > Dim split As String() = Nothing
      > Dim fieldCount As Integer
      > Dim s As String
      >
      > split = line.Split(deli miter)
      > fieldCount = 0
      > For Each s In split
      > Dim y As String = s
      > If y <> "" Then
      > fieldCount = fieldCount + 1
      > End If
      > Next s
      > Dim strAddress As Object = split.GetValue( fieldCount - 1)
      >
      > HTH.
      >
      > Kevin Yu
      > =======
      > "This posting is provided "AS IS" with no warranties, and confers no
      > rights."
      >
      >[/color]

      Comment

      • Kevin Yu [MSFT]

        #4
        RE: issue with split function in vb.net

        You're welcome.

        After modifying the code, it works fine on my machine with both lines.

        Kevin Yu
        =======
        "This posting is provided "AS IS" with no warranties, and confers no
        rights."

        Comment

        • NewToThis

          #5
          RE: issue with split function in vb.net

          It's working with your suggestion. Thank you so much. I have been banging
          my head against the wall on this one for a while.
          --
          Thanks


          "Kevin Yu [MSFT]" wrote:
          [color=blue]
          > You're welcome.
          >
          > After modifying the code, it works fine on my machine with both lines.
          >
          > Kevin Yu
          > =======
          > "This posting is provided "AS IS" with no warranties, and confers no
          > rights."
          >
          >[/color]

          Comment

          • Kevin Yu [MSFT]

            #6
            RE: issue with split function in vb.net

            Thanks for sharing your experience with all the people here. If you have
            any questions, please feel free to post them in the community.

            Kevin Yu
            =======
            "This posting is provided "AS IS" with no warranties, and confers no
            rights."

            Comment

            Working...