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

String.Split versus Strings.Split

P: n/a
The code:
Dim aLine As String = "cat" & vbNewLine & "dog" & vbNewLine & "fox" &
vbNewLine

Dim csvColumns1 As String() = aLine.Split(vbNewLine, vbCr, vbLf)

Dim csvColumns2 As String() = Microsoft.VisualBasic.Strings.Split(aLine,
vbNewLine, -1, CompareMethod.Binary)

returns in csvColumns1 7 elements, cat emptystring dog emptystring fox
emptystring emptystring

returns in csvColumns2 4 elements, cat dog fox emptystring

Questions:

why all emptystrings in the first array?

why the last emptystring in the second array?

how do I most easily get rid of them?

/k
Nov 21 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Here's a simple solution:

Dim nl As String = ControlChars.NewLine
Dim strLine As String = "cat" & nl & "dog" & nl & "fox" & nl
Dim strCSVLineOne() As String = strLine.Split(nl)
For i As Integer = 0 To strCSVLineOne.Length - 1
strCSVLineOne(i) = strCSVLineOne(i).Trim
Next

MessageBox.Show(strCSVLineOne(0) & ControlChars.CrLf & _
strCSVLineOne(1) & ControlChars.CrLf & strCSVLineOne(2))
Nov 21 '05 #2

P: n/a
"kurt sune" <ap*@apa.com> schrieb:
Dim aLine As String = "cat" & vbNewLine & "dog" & vbNewLine & "fox" &
vbNewLine

Dim csvColumns1 As String() = aLine.Split(vbNewLine, vbCr, vbLf)

Dim csvColumns2 As String() = Microsoft.VisualBasic.Strings.Split(aLine,
vbNewLine, -1, CompareMethod.Binary)

returns in csvColumns1 7 elements, cat emptystring dog emptystring fox
emptystring emptystring

returns in csvColumns2 4 elements, cat dog fox emptystring

Questions:

why all emptystrings in the first array?

why the last emptystring in the second array?


'vbNewLine' maps to 'vbCrLf' on Windows systems. 'String.Split' can only
split on single characters, not on string separators. That's why your call
to 'String.Split' will split the string "cat[CR][LF]dog[CR][LF]fox[CR][LF]"
into "cat", "", "dog", "", "fox", "", "". 'Strings.Split' can use strings
as separators. When splitting the string on "[CR][LF]", the resulting array
consists of "cat", "dog", "fox", "". You can use 'ReDim Preserve' to remove
the last item from the array.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #3

P: n/a
Thanks, now I understand,
but where do the last "" comes from in both cases?

I assumed the splitting character/s to be completely removed.

/k

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...

'vbNewLine' maps to 'vbCrLf' on Windows systems. 'String.Split' can only
split on single characters, not on string separators. That's why your call to 'String.Split' will split the string "cat[CR][LF]dog[CR][LF]fox[CR][LF]" into "cat", "", "dog", "", "fox", "", "". 'Strings.Split' can use strings
as separators. When splitting the string on "[CR][LF]", the resulting array consists of "cat", "dog", "fox", "". You can use 'ReDim Preserve' to remove the last item from the array.

Nov 21 '05 #4

P: n/a
Kurt,
| but where do the last "" comes from in both cases?

Split returns any values between the delimiters including between the
beginning or end of the string & a delimiter. It is returning the value
between your last delimiter & the end of the string, which happens to be an
empty string. In your String.Split case you have 2 delimiters in a row (a
carriage return & a line feed), so it is returning the empty string between
those delimiters...

I normally use String.Trim before I split the string if I do not want these
leading or trailing empty strings.
In addition to the other comments on Split, there are three Split functions
in .NET:

Use Microsoft.VisualBasic.Strings.Split if you need to split a string based
on a specific word (string). It is the Split function from VB6.

Use System.String.Split if you need to split a string based on a collection
of specific characters. Each individual character is its own delimiter.

Use System.Text.RegularExpressions.RegEx.Split to split based
on matching patterns.

Hope this helps
Jay

"kurt sune" <ap*@apa.com> wrote in message
news:OB**************@TK2MSFTNGP12.phx.gbl...
| Thanks, now I understand,
| but where do the last "" comes from in both cases?
|
| I assumed the splitting character/s to be completely removed.
|
| /k
|
| "Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
| news:%2****************@tk2msftngp13.phx.gbl...
| >
| > 'vbNewLine' maps to 'vbCrLf' on Windows systems. 'String.Split' can
only
| > split on single characters, not on string separators. That's why your
| call
| > to 'String.Split' will split the string
| "cat[CR][LF]dog[CR][LF]fox[CR][LF]"
| > into "cat", "", "dog", "", "fox", "", "". 'Strings.Split' can use
strings
| > as separators. When splitting the string on "[CR][LF]", the resulting
| array
| > consists of "cat", "dog", "fox", "". You can use 'ReDim Preserve' to
| remove
| > the last item from the array.
| >
| >
|
|
Nov 21 '05 #5

P: n/a
"kurt sune" <ap*@apa.com> wrote in message
news:OE**************@TK2MSFTNGP14.phx.gbl...
The code:
Dim aLine As String = "cat" & vbNewLine & "dog" & vbNewLine & "fox" &
vbNewLine

Dim csvColumns1 As String() = aLine.Split(vbNewLine, vbCr, vbLf)

Dim csvColumns2 As String() = Microsoft.VisualBasic.Strings.Split(aLine,
vbNewLine, -1, CompareMethod.Binary)

returns in csvColumns1 7 elements, cat emptystring dog emptystring fox
emptystring emptystring

returns in csvColumns2 4 elements, cat dog fox emptystring

why all emptystrings in the first array?
[String].Split() breaks the string up using /single-character/ delimiters.
vbCrLf is seen as two, discrete delimiters, so you get a blank entry
between the Cr and the Lf.
why the last emptystring in the second array?


There's a trailing vbNewLine at the end of the string, and Split() sees
that as sitting between "fox" before it and the "blank" item after it.

HTH,
Phill W.
Nov 21 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.