I have a string
which I want to split in two. Usually it is a name, operator and a value. I'd like to split it into name and value. The name can be anything, the value too. What I have, is an array of operators and my idea is to use it as separators:
var input = "name>=2";
var separators = new string[]
{
">",
">=",
};
var result = input.Split(separators, StringSplitOptions.RemoveEmptyEntries);
Code above gives result being name
and =2
. But if I rearrange the order of separators, so the >=
would be first, like this:
var separators = new string[]
{
">=",
">",
};
That way, I'm getting nice name
and 2
which is what I'm trying to achieve. Sadly, keeping the separators in a perfect order is a no go for me. Also, my collection of separators is not immutable. So, I'm thinking maybe I could split the string
with longer separators given precedence over the shorter ones?
Thanks for help!
Here is a related question, explaining why such behaviour occurs in Split()
method.