查找字符串中出现的字符串
本文关键字:字符串 查找 | 更新日期: 2023-09-27 17:53:48
在另一个字符串中查找字符串的最快和最有效的方法是什么?
例如我有这样的文本;
"嘿,@ronald和@tom这个周末我们去哪里?"
但是我想找到以"@"开头的字符串。
可以使用正则表达式
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 s = "Hey @ronald and @tom where are we going this weekend";
var list = s.Split(' ').Where(c => c.StartsWith("@"));
速度:
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 == '@');
检查这里如何计算字符串中字符串的出现次数?
String str = "hallo world"
int pos = str.IndexOf("wo",0)