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