C#, an elegant way to split up string at first (and only first) occurence of two or more whitespaces?
Is there an elegant way to split up string at first (and only first) occurence of two or more whitespaces ? Or at least finding th开发者_如何学运维e index of this two or more whitespaced string.
Thank you very much.
You can construct an instance instead of using the static method and utilize the overload which restricts the number of splits performed:
Regex regex = new Regex(@"\s{2,}");
string[] result = regex.Split(input, 2); // only 1 split, two parts
Check this out: String.Split only on first separator in C#?
Or: http://msdn.microsoft.com/en-us/library/c1bs0eda.aspx
String.Split(separator, number of strings to return)
Use regex splitting as shown here
I guess you'd end up with something like this:
RegexOptions options = RegexOptions.None;
Regex regex = new Regex(@"[ ]{2,}", options);
string[] operands = Regex.Split(operation, regex);
精彩评论