|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
String.Split versus Strings.SplitDim 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 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))
Show quote
Hide quote
"kurt sune" <a**@apa.com> schrieb: 'vbNewLine' maps to 'vbCrLf' on Windows systems. 'String.Split' can only > 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? 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/> 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 "cat[CR][LF]dog[CR][LF]fox[CR][LF]"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 Show quoteHide quote > 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. > > 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 Show quoteHide quote "kurt sune" <a**@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. | > | > | |
Show quote
Hide quote
"kurt sune" <a**@apa.com> wrote in message [String].Split() breaks the string up using /single-character/ delimiters.news:OEPOKERNFHA.2132@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? 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() seesthat as sitting between "fox" before it and the "blank" item after it. HTH, Phill W. |
|||||||||||||||||||||||