473,725 Members | 2,281 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

String.Split versus Strings.Split

The code:
Dim aLine As String = "cat" & vbNewLine & "dog" & vbNewLine & "fox" &
vbNewLine

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

Dim csvColumns2 As String() = Microsoft.Visua lBasic.Strings. Split(aLine,
vbNewLine, -1, CompareMethod.B inary)

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
5 5874
Here's a simple solution:

Dim nl As String = ControlChars.Ne wLine
Dim strLine As String = "cat" & nl & "dog" & nl & "fox" & nl
Dim strCSVLineOne() As String = strLine.Split(n l)
For i As Integer = 0 To strCSVLineOne.L ength - 1
strCSVLineOne(i ) = strCSVLineOne(i ).Trim
Next

MessageBox.Show (strCSVLineOne( 0) & ControlChars.Cr Lf & _
strCSVLineOne(1 ) & ControlChars.Cr Lf & strCSVLineOne(2 ))
Nov 21 '05 #2
"kurt sune" <ap*@apa.com> schrieb:
Dim aLine As String = "cat" & vbNewLine & "dog" & vbNewLine & "fox" &
vbNewLine

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

Dim csvColumns2 As String() = Microsoft.Visua lBasic.Strings. Split(aLine,
vbNewLine, -1, CompareMethod.B inary)

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
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******** ********@tk2msf tngp13.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
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.Visua lBasic.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.S plit if you need to split a string based on a collection
of specific characters. Each individual character is its own delimiter.

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

Hope this helps
Jay

"kurt sune" <ap*@apa.com> wrote in message
news:OB******** ******@TK2MSFTN GP12.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******** ********@tk2msf tngp13.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
"kurt sune" <ap*@apa.com> wrote in message
news:OE******** ******@TK2MSFTN GP14.phx.gbl...
The code:
Dim aLine As String = "cat" & vbNewLine & "dog" & vbNewLine & "fox" &
vbNewLine

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

Dim csvColumns2 As String() = Microsoft.Visua lBasic.Strings. Split(aLine,
vbNewLine, -1, CompareMethod.B inary)

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
8817
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 function that will split it up (on every location where a * occurs in the string). This split function should allocate a 2D array of chars and put the split results in different rows. The listing below shows how I started to work on this. To keep the program simple and help focus the program the...
5
3242
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 need to extract data with. My problem is this... I am encountering a string that has the example below: a, b, c. "d,e,f,g", abcdef
6
6380
by: Senthil | last post by:
Code ---------------------- string Line = "\"A\",\"B\",\"C\",\"D\""; string Line2 = Line.Replace("\",\"","\"\",\"\""); string CSVColumns = Line2.Split("\",\"".ToCharArray());
19
10920
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, multiple whitespace characters create multiple empty strings in the resulting string array.
3
19315
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"; string myStrs = str.Split("||");
2
2192
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 charSeparators = { "=", ">=", "<=" , "!=" }; string s1 = "field != value" result = s1.Split(charSeparators, StringSplitOptions.RemoveEmptyEntries);
33
4681
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 internet when it comes 'when to use what'. Most of articles I read from different experts and programmers tell me that their "gut feelings" for using stringBuilder instead of string concatenation is when the number of string concatunation is more then N ( N varies between 3 to max 15 from...
8
2586
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 this in Python - I'm having some trouble using the .split() function, it doesn't seem to do what I want it to - any ideas? thanks very much for your help. r-sr-
20
3704
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. I'm trying to write a simple text formatting function to make headings proper case -- i.e. first letter of words capitalized. I first tried this...
0
8889
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8752
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9401
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9257
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9116
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4519
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3228
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2637
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2157
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.