Split string separated by multiple spaces, ignorin

2020-03-01 07:31发布

I need to split a string separated by multiple spaces. For example:

"AAAA AAA        BBBB BBB BBB        CCCCCCCC"

I want to split it into these:

"AAAA AAA"   
"BBBB BBB BBB"
"CCCCCCCC"

I tried with this code:

value2 = System.Text.RegularExpressions.Regex.Split(stringvalue, @"\s+");

But not success, I only want to split the string by multiple spaces, not by single space.

标签: c# regex split
3条回答
劫难
2楼-- · 2020-03-01 08:17
value2 = System.Text.RegularExpressions.Regex.Split( stringvalue, @"\s{2,}");
查看更多
时光不老,我们不散
3楼-- · 2020-03-01 08:20

+ means "one or more", so a single space would qualify as a separator. If you want to require more than once, use {m,n}:

value2 = System.Text.RegularExpressions.Regex.Split( stringvalue, @"\s{2,}");

The {m,n} expression requires the expression immediately prior to it match m to n times, inclusive. Only one limit is required. If the upper limit is missing, it means "m or more repetitions".

查看更多
老娘就宠你
4楼-- · 2020-03-01 08:21
value2 = System.Text.RegularExpressions.Regex.Split( stringvalue, @"\s\s+");
查看更多
登录 后发表回答