String.Split versus Strings.Split

K

kurt sune

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
 
C

Crouchie1998

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))
 
H

Herfried K. Wagner [MVP]

kurt sune said:
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.
 
K

kurt sune

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
 
J

Jay B. Harlow [MVP - Outlook]

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



| 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
|
| | >
| > '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.
| >
| >
|
|
 
P

Phill. W

kurt sune said:
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.
 

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