Connecting Tech Pros Worldwide Forums | Help | Site Map

Split

Itzik
Guest
 
Posts: n/a
#1: Nov 16 '05
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





Bjorn Abelli
Guest
 
Posts: n/a
#2: Nov 16 '05

re: Split


"Itzik" wrote...
[color=blue]
> 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());[/color]

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


Jay B. Harlow [MVP - Outlook]
Guest
 
Posts: n/a
#3: Nov 16 '05

re: Split


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:
[color=blue]
> string str = "aa a - bb-b - ccc"[/color]
[color=blue]
> string del = " - "[/color]

string [] fields = Strings.Split(str, del, -1, CompareMethod.Binary);

Hope this helps
Jay


"Itzik" <itzikm@pisgasys.co.il> wrote in message
news:ejFpoUMxEHA.2572@tk2msftngp13.phx.gbl...[color=blue]
> 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
>
>
>
>[/color]


Justin Rogers
Guest
 
Posts: n/a
#4: Nov 16 '05

re: Split


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" <itzikm@pisgasys.co.il> wrote in message
news:ejFpoUMxEHA.2572@tk2msftngp13.phx.gbl...[color=blue]
> 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
>
>
>
>[/color]


Jay B. Harlow [MVP - Outlook]
Guest
 
Posts: n/a
#5: Nov 16 '05

re: Split


Justin,
COOL! I'll have to add that one to my little FAQ.

Thanks
Jay


"Justin Rogers" <Justin@games4dotnet.com> wrote in message
news:OC0IXmPxEHA.2540@TK2MSFTNGP09.phx.gbl...[color=blue]
> 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" <itzikm@pisgasys.co.il> wrote in message
> news:ejFpoUMxEHA.2572@tk2msftngp13.phx.gbl...[color=green]
>> 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
>>
>>
>>
>>[/color]
>
>[/color]


Closed Thread