1
A
回答
3
您可以使用正则表达式。
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
-1
0
1
你需要使用正则表达式:
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);
}
0
如果你是速度之后:
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. 字符串中出现字符
- 2. 字符串出现在MySQL
- 3. 发现字符串中给定子字符串的出现次数
- 4. 在MySQL中查找字符串中子字符串的出现?
- 5. 字符串出现在另一个字符串中的次数
- 6. 发现在SQL Server中的字符串中的字符的出现次数
- 7. 获取第二个字符/字符串出现的字符串
- 8. 发生的最大数目的字符出现在字符串
- 9. Java字符串计数字符出现
- 10. Borland字符串::发现bug
- 11. 字符串不会出现
- 12. 删除字符串中所有子字符串的出现
- 13. 字符串中子字符串出现的性能
- 14. 字符串中出现的所有子字符串
- 15. 字符串中子字符串的出现次数(Java)
- 16. 替换字符串中出现的第n个子字符串
- 17. 获取字符串列表中字符串的出现次数。
- 18. PHP查找字符串中出现的所有子字符串
- 19. 第一次替换字符串中出现的字符串VB.NET
- 20. 获取字符串中所有子字符串的出现
- 21. 替换字符串中最后出现的子字符串
- 22. 查找字符串中出现的所有子字符串
- 23. 爪哇 - 查找字符串的出现在一个字符串
- 24. 计数字符串的出现在一个字符串
- 25. 如何在字符串中出现多次的字符上分割字符串
- 26. 在一个字符串中搜索其他字符串的字符出现
- 27. 发现其中id字符串包含特定字符串
- 28. 特定字符串出现时的Java子字符串字符串
- 29. 计算字符串向量中字符串出现次数
- 30. 使用“in”检查字符串中是否出现字符串
+1为最可重复使用的解决方案imo – Nicolas78 2011-04-30 10:11:30
感谢这为我工作 – pmillio 2011-04-30 10:50:52