用多个空格分隔的分割字符串,忽略单个空格

时间:2022-07-03 21:38:05

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.

但不是成功,我只想把字符串分割成多个空格,而不是单个空格。

3 个解决方案

#1


25  

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

+表示“一个或多个”,因此单个空格可以作为分隔符。如果您需要不止一次,请使用{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".

{m,n}表达式要求在匹配m到n次之前的表达式,包括。只有一个限制是必需的。如果没有上限,则表示“m或更多重复”。

#2


2  

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

#3


2  

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

#1


25  

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

+表示“一个或多个”,因此单个空格可以作为分隔符。如果您需要不止一次,请使用{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".

{m,n}表达式要求在匹配m到n次之前的表达式,包括。只有一个限制是必需的。如果没有上限,则表示“m或更多重复”。

#2


2  

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

#3


2  

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