Connecting Tech Pros Worldwide Forums | Help | Site Map

String.Split versus Strings.Split

kurt sune
Guest
 
Posts: n/a
#1: Nov 21 '05
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



Crouchie1998
Guest
 
Posts: n/a
#2: Nov 21 '05

re: String.Split versus Strings.Split


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


Herfried K. Wagner [MVP]
Guest
 
Posts: n/a
#3: Nov 21 '05

re: String.Split versus Strings.Split


"kurt sune" <apa@apa.com> schrieb:[color=blue]
> 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?[/color]

'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/>

kurt sune
Guest
 
Posts: n/a
#4: Nov 21 '05

re: String.Split versus Strings.Split


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]" <hirf-spam-me-here@gmx.at> wrote in message
news:%23c6fp8RNFHA.1096@tk2msftngp13.phx.gbl...[color=blue]
>
> 'vbNewLine' maps to 'vbCrLf' on Windows systems. 'String.Split' can only
> split on single characters, not on string separators. That's why your[/color]
call[color=blue]
> to 'String.Split' will split the string[/color]
"cat[CR][LF]dog[CR][LF]fox[CR][LF]"[color=blue]
> into "cat", "", "dog", "", "fox", "", "". 'Strings.Split' can use strings
> as separators. When splitting the string on "[CR][LF]", the resulting[/color]
array[color=blue]
> consists of "cat", "dog", "fox", "". You can use 'ReDim Preserve' to[/color]
remove[color=blue]
> the last item from the array.
>
>[/color]


Jay B. Harlow [MVP - Outlook]
Guest
 
Posts: n/a
#5: Nov 21 '05

re: String.Split versus Strings.Split


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" <apa@apa.com> wrote in message
news:OBQsGySNFHA.3760@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]" <hirf-spam-me-here@gmx.at> wrote in message
| news:%23c6fp8RNFHA.1096@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.
| >
| >
|
|


Phill. W
Guest
 
Posts: n/a
#6: Nov 21 '05

re: String.Split versus Strings.Split


"kurt sune" <apa@apa.com> wrote in message
news:OEPOKERNFHA.2132@TK2MSFTNGP14.phx.gbl...[color=blue]
> 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?[/color]

[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.
[color=blue]
> why the last emptystring in the second array?[/color]

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.


Closed Thread