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);