Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Splitting string based on uneven number of white spaces

I need to split a string that looks like this

1052 root         0 SW<  [hwevent]

into the following

1052
root
0
SW<
[hwevent]

sure, I could just whip up a forloop and compare character indices with white spaces and when the occurrence is not a white space, add the occurrence to a new string array but I feel like this is a really dirty way to do this.

What is a good way to split this string? Regex perhaps?

like image 417
l46kok Avatar asked Nov 29 '22 08:11

l46kok


2 Answers

You may use StringSplitOptions.RemoveEmptryEntries

string strtemp = "1052 root         0 SW<  [hwevent]";
string[] array = strtemp.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
like image 191
Habib Avatar answered Dec 05 '22 16:12

Habib


Yes, regex:

splitArray = Regex.Split(subjectString, @"\s+");

Explanation:

\s+ matches one or more whitespace characters at once, so it splits on any (positive) number of whitespace characters.

like image 24
Tim Pietzcker Avatar answered Dec 05 '22 17:12

Tim Pietzcker