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 5 5666
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))
"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/>
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.
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.
| >
| >
|
|
"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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Simon Schaap |
last post by:
Hello,
I have encountered a strange problem and I hope you can help me to
understand it. What I want to do is to pass an array of chars to a...
|
by: Ann Marinas |
last post by:
Happy New Year to all! :D
I am currently developoing an application that imports data from a CSV file.
Each comma represents an array item that I...
|
by: Senthil |
last post by:
Code
----------------------
string Line = "\"A\",\"B\",\"C\",\"D\"";
string Line2 = Line.Replace("\",\"","\"\",\"\"");
string CSVColumns =...
|
by: David Logan |
last post by:
We need an additional function in the String class. We need the ability
to suppress empty fields, so that we can more effectively parse. Right
now,...
|
by: Hetal Shah |
last post by:
It is a C# code.
I have a string like, one||two||three
I want to split it into one, two and three.
string str = "one||two||three";...
|
by: Digital Fart |
last post by:
following code would split a string "a != b" into 2 strings "a" and
"b".
but is there a way to know what seperator was used?
string...
|
by: genc_ymeri |
last post by:
Hi over there,
Propably this subject is discussed over and over several times. I did google
it too but I was a little bit surprised what I read on...
|
by: ronrsr |
last post by:
I have a single long string - I'd like to split it into a list of
unique keywords. Sadly, the database wasn't designed to do this, so I
must do...
|
by: Xcriber51 |
last post by:
Hi --
I'm not entirely familiar with the norms and standard libraries of
JavaScript so if the answer to this is yesterday's news, please ignore....
|
by: Kemmylinns12 |
last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
|
by: Naresh1 |
last post by:
What is WebLogic Admin Training?
WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
|
by: antdb |
last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine
In the overall architecture, a new "hyper-convergence" concept was...
|
by: Matthew3360 |
last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function.
Here is my code.
...
|
by: Matthew3360 |
last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
|
by: AndyPSV |
last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
|
by: Arjunsri |
last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
|
by: Oralloy |
last post by:
Hello Folks,
I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA.
My problem (spelled failure) is with the...
|
by: BLUEPANDA |
last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...
| |