查找匹配的单词 C#

发布于 2024-11-09 04:08:11 字数 775 浏览 0 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦中的蝴蝶 2024-11-16 04:08:11

如果您只是想查看较长的字符串是否包含特定的较短字符串,请使用 String.Contains

对于你的例子:

string[] urlStrings = new string[] 
{
    @"http://www.vkeong.com/2011/food-drink/heng-bak-kut-teh-delights-taman-kepong/#comments"
    @"http://www.vkeong.com/2009/food-drink/sen-kee-duck-satay-taman-desa-jaya-kepong"
    @"http://www.vkeong.com/2008/food-drink/nasi-lemak-wai-sik-kai-kepong-baru/"
}

foreach(String url in urlStrings)
{
    if(url.Contains("nasi-lemak"))
    {
        //Your code to handle a match here.
    }
}

If you're just looking to see if a longer string contains a specific shorter string, use String.Contains.

For your example:

string[] urlStrings = new string[] 
{
    @"http://www.vkeong.com/2011/food-drink/heng-bak-kut-teh-delights-taman-kepong/#comments"
    @"http://www.vkeong.com/2009/food-drink/sen-kee-duck-satay-taman-desa-jaya-kepong"
    @"http://www.vkeong.com/2008/food-drink/nasi-lemak-wai-sik-kai-kepong-baru/"
}

foreach(String url in urlStrings)
{
    if(url.Contains("nasi-lemak"))
    {
        //Your code to handle a match here.
    }
}
饮惑 2024-11-16 04:08:11

您需要 String.IndexOf 方法。

foreach(string url in url_list)
{
    if(url.IndexOf("nasi-lemak") != -1)
    {
        // Found!
    }
}

You want the String.IndexOf method.

foreach(string url in url_list)
{
    if(url.IndexOf("nasi-lemak") != -1)
    {
        // Found!
    }
}
佞臣 2024-11-16 04:08:11

当然我们还需要 LINQ 答案:)

var matches = urlStrings.Where(s => s.Contains("nasi-lemak"));

// or if you prefer query form. This is really the same as above
var matches2 = from url in urlStrings
               where url.Contains("nasi-lemak")
               select url;

// Now you can use matches or matches2 in a foreach loop
foreach (var matchingUrl in matches)
     DoStuff(matchingUrl);

Surely we also need a LINQ answer :)

var matches = urlStrings.Where(s => s.Contains("nasi-lemak"));

// or if you prefer query form. This is really the same as above
var matches2 = from url in urlStrings
               where url.Contains("nasi-lemak")
               select url;

// Now you can use matches or matches2 in a foreach loop
foreach (var matchingUrl in matches)
     DoStuff(matchingUrl);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文