471,108 Members | 1,353 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,108 software developers and data experts.

.net 2.0 split and knowing which seperator was used to split the string

following code would split a string "a != b" into 2 strings "a" and
"b".
but is there a way to know what seperator was used?

string[] charSeparators = { "=", ">=", "<=" , "!=" };

string s1 = "field != value"

result = s1.Split(charSeparators,
StringSplitOptions.RemoveEmptyEntries);
Jan 16 '06 #1
2 2007
DF,

No, there is no way with that call to know which separator was used.

What you could do is make individual calls to split, using one delimiter
at a time, and then determine if a split was used based on the number of
elements returned.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Digital Fart" <pr*********@angelfire.com> wrote in message
news:pu********************************@4ax.com...
following code would split a string "a != b" into 2 strings "a" and
"b".
but is there a way to know what seperator was used?

string[] charSeparators = { "=", ">=", "<=" , "!=" };

string s1 = "field != value"

result = s1.Split(charSeparators,
StringSplitOptions.RemoveEmptyEntries);

Jan 16 '06 #2
The short answer is, all of them are used. When you pass an array of strings
to the String.Splite method, all of the strings in the array are used. If,
for example, you had a string like the following:

"a != b && b <= c"

The string would be split into 3 substrings.

So, first, there is a flaw in your reasoning. You're apparently assuming
that the string to split will have one and only one of the substrings in the
array in it. This may be true, as you created the business rules, and wrote
the code, but neither I nor the String.Split method can know this.

Now, if you were to inform me that you are working with a business rule that
enforces this, there are several alternatives available to you, all of which
use the String.IndexOf method, and some of which may employ the String.Split
method, but not necessarily by any means. In fact, the String.Split method
would not be employed in the best of these solutions.

To break it down for you, first, you need to identify which one (and only
one) of the strings in the array is in the string to split. This involves
looping through the array. Example:

string testString = "a != b";
int i, index;
string start, end;
string[] charSeparators = { "=", ">=", "<=" , "!=" };
for (i = 0; i < charSeparators.Length; i++)
{
if ((index = testString.IndexOf(charSeparators[i])) > -1)
{
}
}

When the statement evaluates to true, you've found the separator. However,
you don't need String.Split at this point. You can split the string with the
substring and the index:

if ((index = testString.IndexOf(charSeparators[i])) > -1)
{
start = testString.Substring(0, index);
end = testString.SubString(index + charSeparators[i].Length);
break;
}

At this point, "i" is the index in charSeparators of the substring found.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
You can lead a fish to a bicycle,
but it takes a very long time,
and the bicycle has to *want* to change.

"Digital Fart" <pr*********@angelfire.com> wrote in message
news:pu********************************@4ax.com...
following code would split a string "a != b" into 2 strings "a" and
"b".
but is there a way to know what seperator was used?

string[] charSeparators = { "=", ">=", "<=" , "!=" };

string s1 = "field != value"

result = s1.Split(charSeparators,
StringSplitOptions.RemoveEmptyEntries);

Jan 16 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by Carlos Ribeiro | last post: by
5 posts views Thread by oliver | last post: by
5 posts views Thread by Arjen | last post: by
2 posts views Thread by Teis Draiby | last post: by
4 posts views Thread by Jesper Denmark | last post: by
4 posts views Thread by William Stacey [MVP] | last post: by
1 post views Thread by randomtalk | last post: by
3 posts views Thread by Ondrej Baudys | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.