Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split only on multiple white spaces C#

Tags:

c#

.net

Lets say i have a string:

"Joe Doe     is    not    here"

I want to split this string only where it is multiple white spaces but keep the Joe Doe as one substring.

So that the result would be:

string[] result={"Joe Doe","is","not","Here"}
like image 310
Oyvind Avatar asked Jun 05 '26 08:06

Oyvind


2 Answers

Use a Regex.Split with @"\s{2,}" as the pattern - which will split wherever there are 2 or more whitespace characters.

like image 96
PhonicUK Avatar answered Jun 07 '26 22:06

PhonicUK


Regex.Split(input, @"\s{2,}")

This regex requires min. 2 spaces.

like image 23
usr Avatar answered Jun 07 '26 23:06

usr



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!