I want to get white spaces which are greater than 1 space long.
我希望获得大于1个空格的空白区域。
The following gets me the null chars between each letter, and also the white spaces. However I only want to extract the two white spaces string between c
and d
, and the 3 white spaces string between f
and g
.
下面给我每个字母之间的空字符,以及白色空格。但是我只想提取c和d之间的两个空格字符串,以及f和g之间的3个空格字符串。
string b = "ab c def gh";
List<string> c = Regex.Split(b, @"[^\s]").ToList();
UPDATE: The following works, but I'm looking for a more elegant way of achieving this:
更新:以下工作,但我正在寻找一种更优雅的方式来实现这一目标:
c.RemoveAll(x => x == "" || x == " ");
The desired result would be a List<string>
containing " "
and " "
所需的结果将是包含“”和“”的List
5 个解决方案
#1
5
If you want List<String>
as a result you could execute this Linq query
如果您希望List
string b = "ab c def gh";
List<String> c = Regex
.Matches(b, @"\s{2,}")
.OfType<Match>()
.Select(match => match.Value)
.ToList();
#2
3
This should give you your desired List.
这应该会给你你想要的清单。
string b = "ab c def gh";
var regex = new Regex(@"\s\s+");
var result = new List<string>();
foreach (Match m in regex.Matches(b))
result.Add(m.Value);
#3
2
If all you are interested in are these groups of whitespaces, you could use
如果您感兴趣的是这些空白组,您可以使用
foreach(var match in Regex.Matches(b, @"\s\s+")) {
// ... do something with match
}
This guarantees that you will match at least 2 whitespaces.
这可以保证您将匹配至少2个空格。
#4
2
Rather than splitting using a Regex, try using Regex.Matches
to get all items matching your pattern - in this case I've used a pattern to match two or more whitespace characters, which I think is what you want?
而不是使用正则表达式进行拆分,尝试使用Regex.Matches来获取与您的模式匹配的所有项目 - 在这种情况下,我使用了一个模式来匹配两个或更多的空白字符,我认为这是你想要的?
var matchValues = Regex.Matches("ab c def gh", "\\s\\s+")
.OfType<Match>().Select(m => m.Value).ToList();
Annoyingly, the MatchCollection
returned by Regex.Matches
isn't IEnumerable<Match>
, hence the need to use OfType<>
in the LINQ expression.
令人讨厌的是,Regex.Matches返回的MatchCollection不是IEnumerable
#5
1
You can use the following single line :
您可以使用以下单行:
var list =Regex.Matches(value,@"[ ]{2,}").Cast<Match>().Select(match => match.Value).ToList();
Hope it will help you.
希望它会对你有所帮助。
#1
5
If you want List<String>
as a result you could execute this Linq query
如果您希望List
string b = "ab c def gh";
List<String> c = Regex
.Matches(b, @"\s{2,}")
.OfType<Match>()
.Select(match => match.Value)
.ToList();
#2
3
This should give you your desired List.
这应该会给你你想要的清单。
string b = "ab c def gh";
var regex = new Regex(@"\s\s+");
var result = new List<string>();
foreach (Match m in regex.Matches(b))
result.Add(m.Value);
#3
2
If all you are interested in are these groups of whitespaces, you could use
如果您感兴趣的是这些空白组,您可以使用
foreach(var match in Regex.Matches(b, @"\s\s+")) {
// ... do something with match
}
This guarantees that you will match at least 2 whitespaces.
这可以保证您将匹配至少2个空格。
#4
2
Rather than splitting using a Regex, try using Regex.Matches
to get all items matching your pattern - in this case I've used a pattern to match two or more whitespace characters, which I think is what you want?
而不是使用正则表达式进行拆分,尝试使用Regex.Matches来获取与您的模式匹配的所有项目 - 在这种情况下,我使用了一个模式来匹配两个或更多的空白字符,我认为这是你想要的?
var matchValues = Regex.Matches("ab c def gh", "\\s\\s+")
.OfType<Match>().Select(m => m.Value).ToList();
Annoyingly, the MatchCollection
returned by Regex.Matches
isn't IEnumerable<Match>
, hence the need to use OfType<>
in the LINQ expression.
令人讨厌的是,Regex.Matches返回的MatchCollection不是IEnumerable
#5
1
You can use the following single line :
您可以使用以下单行:
var list =Regex.Matches(value,@"[ ]{2,}").Cast<Match>().Select(match => match.Value).ToList();
Hope it will help you.
希望它会对你有所帮助。