Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C#, an elegant way to split up string at first (and only first) occurence of two or more whitespaces?

Tags:

string

c#

split

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.

like image 895
Primoz Avatar asked Dec 02 '25 04:12

Primoz


1 Answers

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
like image 55
user7116 Avatar answered Dec 03 '25 17:12

user7116