Hello,

I am trying to use string.split to break up a string into fields. It seems
as though, when using space as a delimiter, the method fails to perform as
desired when more than one space delimits two fields.

Am I interpreting this correctly? In perl, the following string:

"this that and the other thing"

would split into 6 elements. The .NET makes a fields for each space?

Is there a way to handle this (other than regular expressions?)

Re: Split - not like perl? by David

David
Thu Mar 24 16:17:57 CST 2005


"mklapp" <mklapp@discussions.microsoft.com> wrote in message
news:9EEEC93E-2AD5-4926-B9DB-D30AF7612A3B@microsoft.com...
> Hello,
>
> I am trying to use string.split to break up a string into fields. It
> seems
> as though, when using space as a delimiter, the method fails to perform as
> desired when more than one space delimits two fields.
>
> Am I interpreting this correctly? In perl, the following string:
>
> "this that and the other thing"
>
> would split into 6 elements. The .NET makes a fields for each space?
>
> Is there a way to handle this (other than regular expressions?)
>
What's wrong with regular expressions?

Anyway, of course it can be coded:

static string[] Split(string s)
{
ArrayList l = new ArrayList();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < s.Length; i++)
{
char c = s[i];
if (c == ' ')
{
if (sb.Length > 0)
{
l.Add(sb.ToString());
sb.Length = 0;
}
}
else
{
sb.Append(c);
}
}
if (sb.Length > 0)
{
l.Add(sb.ToString());
sb.Length = 0;
}
return (string[])l.ToArray(typeof(string));
}

David



RE: Split - not like perl? by katyk

katyk
Fri Mar 25 14:01:24 CST 2005


From: =?Utf-8?B?bWtsYXBw?= <mklapp@discussions.microsoft.com>
| Am I interpreting this correctly? In perl, the following string:
|
| "this that and the other thing"
|
| would split into 6 elements. The .NET makes a fields for each space?

You are interpreting the behavior correctly; there will be a separate
element for each space.
In Whidbey, we will be adding a String.Split overload that does what you
want.
See <http://blogs.msdn.com/bclteam/archive/2005/02/14/372636.aspx>

Katy


Re: Split - not like perl? by Anubhav

Anubhav
Sat Mar 26 22:53:52 CST 2005

either u have to write ur own split or wait till widbey
:)

"mklapp" <mklapp@discussions.microsoft.com> wrote in message
news:9EEEC93E-2AD5-4926-B9DB-D30AF7612A3B@microsoft.com...
> Hello,
>
> I am trying to use string.split to break up a string into fields. It
> seems
> as though, when using space as a delimiter, the method fails to perform as
> desired when more than one space delimits two fields.
>
> Am I interpreting this correctly? In perl, the following string:
>
> "this that and the other thing"
>
> would split into 6 elements. The .NET makes a fields for each space?
>
> Is there a way to handle this (other than regular expressions?)
>
>