我有3个字符串,我想找到匹配
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/
for each link above=="nasi-lemak"
{
found!
}
答案 0 :(得分:2)
如果您只是想查看较长的字符串是否包含特定的较短字符串,请使用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.
}
}
答案 1 :(得分:1)
您需要String.IndexOf方法。
foreach(string url in url_list)
{
if(url.IndexOf("nasi-lemak") != -1)
{
// Found!
}
}
答案 2 :(得分:1)
当然我们还需要一个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);