拆分字符串由多个空格隔开,忽略单个空格 [英] Split string separated by multiple spaces, ignoring single spaces

查看:1045
本文介绍了拆分字符串由多个空格隔开,忽略单个空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要拆分多个空格隔开的字符串。例如:

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

"AAAA AAA        BBBB BBB BBB        CCCCCCCC"

我想将它拆分成以下:

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

我试着用这个代码

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.

推荐答案

+ 表示一个或多个,因此单个空格会资格作为一个分隔符。如果你想需要一次以上,使用 {M,N}

+ 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,}");



{M,N} 表达需要紧接其表达式匹配 M N 倍,具有包容性。只有一个限制是必需的。如果上限丢失,它意味着 M 或多次重复。

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".

这篇关于拆分字符串由多个空格隔开,忽略单个空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆