Primoz
Primoz

Reputation: 4331

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 the index of this two or more whitespaced string.

Thank you very much.

Upvotes: 3

Views: 4492

Answers (3)

user7116
user7116

Reputation: 64078

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

Upvotes: 4

Tys
Tys

Reputation: 3610

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

Upvotes: 0

TheITGuy
TheITGuy

Reputation: 722

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)

Upvotes: 2

Related Questions