Split - not like perl?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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?)
 
mklapp said:
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;
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
 
From: =?Utf-8?B?bWtsYXBw?= <[email protected]>
| 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
 
Back
Top