By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,665 Members | 1,372 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,665 IT Pros & Developers. It's quick & easy.

Split

P: n/a
can i split this string

string str = "aa a - bb-b - ccc"

with this delimiter

string del = " - "

i want recieve 3 items : "aa a" , "bb-b" , "ccc"

i use this syntax :
string str = "aa a - bb-b - ccc";

string del = " - ";

string[] temp = str.Split(del.ToCharArray());

now is return me 9 items : "aa" , "a" , "" , "" , "bb" , "b" , "" , "" ,
"ccc"

Thanks


Nov 16 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
"Itzik" wrote...
can i split this string

string str = "aa a - bb-b - ccc"

with this delimiter

string del = " - "

i want recieve 3 items : "aa a" , "bb-b" , "ccc"

i use this syntax :
string str = "aa a - bb-b - ccc";

string del = " - ";

string[] temp = str.Split(del.ToCharArray());


Here's one mistake. What you do here is to split the *delimiter* into an
array of chars. The resulting array of ToCharArray is:

{' ', '-', ' ')

Which means that you split the string by both spaces and hyphens.

Another "problem" is that String.Split only works with just characters as
delimiters, but underneath it really makes use of another method:
Regex.Split.

With that you can use the *string* "del" as the delimiter, e.g.:

using System.Text.RegularExpressions;

...

string str = "aa a - bb-b - ccc";

string del = " - ";

string[] temp = Regex.Split(str, del);
// Bjorn A
Nov 16 '05 #2

P: n/a
Itzik,
As Bjorn stated, String.Split splits based on individual characters. If you
want to split based on words I would recommend Strings.Split or RegEx.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.
By referencing the Microsoft.VisualBasic assembly in C# you can use the
Strings.Split function to split a string based on a word.

Something like:
string str = "aa a - bb-b - ccc" string del = " - "
string [] fields = Strings.Split(str, del, -1, CompareMethod.Binary);

Hope this helps
Jay
"Itzik" <it****@pisgasys.co.il> wrote in message
news:ej**************@tk2msftngp13.phx.gbl... can i split this string

string str = "aa a - bb-b - ccc"

with this delimiter

string del = " - "

i want recieve 3 items : "aa a" , "bb-b" , "ccc"

i use this syntax :
string str = "aa a - bb-b - ccc";

string del = " - ";

string[] temp = str.Split(del.ToCharArray());

now is return me 9 items : "aa" , "a" , "" , "" , "bb" , "b" , "" , "" ,
"ccc"

Thanks

Nov 16 '05 #3

P: n/a
http://weblogs.asp.net/justin_rogers.../14/89545.aspx

A very fast string splitter that thwomps both Regex, because of the
overhead, and VB.Split, because they have some weird heuristics.
--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers
"Itzik" <it****@pisgasys.co.il> wrote in message
news:ej**************@tk2msftngp13.phx.gbl...
can i split this string

string str = "aa a - bb-b - ccc"

with this delimiter

string del = " - "

i want recieve 3 items : "aa a" , "bb-b" , "ccc"

i use this syntax :
string str = "aa a - bb-b - ccc";

string del = " - ";

string[] temp = str.Split(del.ToCharArray());

now is return me 9 items : "aa" , "a" , "" , "" , "bb" , "b" , "" , "" ,
"ccc"

Thanks

Nov 16 '05 #4

P: n/a
Justin,
COOL! I'll have to add that one to my little FAQ.

Thanks
Jay
"Justin Rogers" <Ju****@games4dotnet.com> wrote in message
news:OC**************@TK2MSFTNGP09.phx.gbl...
http://weblogs.asp.net/justin_rogers.../14/89545.aspx

A very fast string splitter that thwomps both Regex, because of the
overhead, and VB.Split, because they have some weird heuristics.
--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers
"Itzik" <it****@pisgasys.co.il> wrote in message
news:ej**************@tk2msftngp13.phx.gbl...
can i split this string

string str = "aa a - bb-b - ccc"

with this delimiter

string del = " - "

i want recieve 3 items : "aa a" , "bb-b" , "ccc"

i use this syntax :
string str = "aa a - bb-b - ccc";

string del = " - ";

string[] temp = str.Split(del.ToCharArray());

now is return me 9 items : "aa" , "a" , "" , "" , "bb" , "b" , "" , "" ,
"ccc"

Thanks


Nov 16 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.