C# 查找字符串中出现的字符串
在另一个字符串中查找字符串的最快和最有效的方法是什么 比如我有这篇课文 “嘿,罗纳德和汤姆,这个周末我们去哪里?” 但是,我想找到以“@”开头的字符串。试试这个:C# 查找字符串中出现的字符串,c#,C#,在另一个字符串中查找字符串的最快和最有效的方法是什么 比如我有这篇课文 “嘿,罗纳德和汤姆,这个周末我们去哪里?” 但是,我想找到以“@”开头的字符串。试试这个: string s = "Hey @ronald and @tom where are we going this weekend"; var list = s.Split(' ').Where(c => c.StartsWith("@")); 试试这个: string s = "Hey @ronald and @tom whe
string s = "Hey @ronald and @tom where are we going this weekend";
var list = s.Split(' ').Where(c => c.StartsWith("@"));
试试这个:
string s = "Hey @ronald and @tom where are we going this weekend";
var list = s.Split(' ').Where(c => c.StartsWith("@"));
您可以使用正则表达式
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 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 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 == '@');
如果您追求速度,请勾选此处:
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 == '@');
检查这里问题仍然是,如果你期待不止一场比赛,该怎么办,如示例所示-这是效率发挥作用的地方灌输问题是,如果你期待不止一场比赛,该怎么办,如示例所示-这是效率发挥作用的地方是,如果你想删除它们,我想
Regex
将是最好的选择。是的,如果你想删除它们,我想Regex
将是最好的选择。