By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,759 Members | 1,727 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,759 IT Pros & Developers. It's quick & easy.

issue with split function in vb.net

P: n/a
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.
--
Thanks
Jul 21 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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."

Jul 21 '05 #2

P: n/a
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:
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."

Jul 21 '05 #3

P: n/a
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."

Jul 21 '05 #4

P: n/a
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:
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."

Jul 21 '05 #5

P: n/a
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."

Jul 21 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.