【问题标题】:Get sum of multiple contains获取多个包含的总和
【发布时间】:2016-07-18 04:20:54
【问题描述】:

我有一个字符串,例如“这是一个示例”。在这句中是另一个词-“小测试”。我想知道这句话中有多少次是“a little test”。有什么办法吗?谢谢

string tmp = "This a little test is a little test an example. a little test";
int sum = ...... (the count of "a little test")

【问题讨论】:

标签: c# string count sum contains


【解决方案1】:

你可以这样做:

int sum = Regex.Matches(tmp, "a little test").Count;

Check it in a fiddle

如果您的“搜索词”可能包含可用于正则表达式的字符,您可能需要使用Regex.Escape

int sum = Regex.Matches(tmp, Regex.Escape(@"my search term with $pecia| $ymb\0Ls")).Count;

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-25
  • 2020-10-27
  • 1970-01-01
  • 2019-01-16
相关资源
最近更新 更多