Split

  • Thread starter Thread starter Itzik
  • Start date Start date
I

Itzik

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
 
...
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
 
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 said:
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
 
Justin,
COOL! I'll have to add that one to my little FAQ.

Thanks
Jay


Justin Rogers said:
http://weblogs.asp.net/justin_rogers/archive/2004/03/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 said:
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
 
Back
Top