在字符串中发现字符串的出现
问题描述:
答
您可以使用正则表达式。
string test = "Hey @ronald and @tom where are we going this weekend";
Regex regex = new Regex(@"@[\S]+");
MatchCollection matches = regex.Matches(test);
foreach (Match match in matches)
{
Console.WriteLine(match.Value);
}
将输出:
@ronald
@tom
答
你需要使用正则表达式:
string data = "Hey @ronald and @tom where are we going this weekend";
var result = Regex.Matches(data, @"@\w+");
foreach (var item in result)
{
Console.WriteLine(item);
}
答
如果你是速度之后:
string source = "Hey @ronald and @tom where are we going this weekend";
int count = 0;
foreach (char c in source)
if (c == '@') count++;
如果你想要一个班轮:
string source = "Hey @ronald and @tom where are we going this weekend";
var count = source.Count(c => c == '@');
这里How would you count occurrences of a string within a string?
+1为最可重复使用的解决方案imo – Nicolas78 2011-04-30 10:11:30
感谢这为我工作 – pmillio 2011-04-30 10:50:52