issue with split function in vb.net

G

Guest

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.ToCharArray
Dim split As String() = Nothing
Dim fieldCount As Integer
Dim s As String

split = line.Split(delimiter, line.Split.GetUpperBound(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.
 
K

Kevin Yu [MSFT]

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 misunderstanding, please feel free to let me
know.

I think the part "line.Split.GetUpperBound(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.ToCharArray
Dim split As String() = Nothing
Dim fieldCount As Integer
Dim s As String

split = line.Split(delimiter)
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."
 
G

Guest

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.
 
K

Kevin Yu [MSFT]

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."
 
G

Guest

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.
 
K

Kevin Yu [MSFT]

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."
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top