Splitting string based on uneven number of white spaces

前端 未结 2 1692
没有蜡笔的小新
没有蜡笔的小新 2020-12-07 04:53

I need to split a string that looks like this

1052 root         0 SW<  [hwevent]

into the following

1052
root
0
SW<
[         


        
相关标签:
2条回答
  • 2020-12-07 05:00

    You may use StringSplitOptions.RemoveEmptryEntries

    string strtemp = "1052 root         0 SW<  [hwevent]";
    string[] array = strtemp.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
    
    0 讨论(0)
  • 2020-12-07 05:19

    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.

    0 讨论(0)
提交回复
热议问题