[英]How to retrieve the value passed in string.Contains method?
是否可以检索在string.Contains方法中传递的值?
使用场景如下所示:
foreach (string x in myList)
{
if (x.Contains("Salt") || x.Contains("Sugar") || x.Contains("Pepper"))
{
MessageBox.Show("The ingredient found in this line of myList is: " + //the ingredient found)
}
}
目的是避免必须重复代码。 现在,我必须执行以下操作才能获得所需的结果:
foreach (string x in myList)
{
if (x.Contains("Salt"))
{
MessageBox.Show("The ingredient found in this line of myList is: Salt");
}
if (x.Contains("Sugar"))
{
MessageBox.Show("The ingredient found in this line of myList is: Sugar");
}
if (x.Contains("Pepper"))
{
MessageBox.Show("The ingredient found in this line of myList is: Pepper");
}
}
如果不可能,还有其他选择吗?
提前致谢!
您可以使用FirstOrDefault LINQ扩展方法并编写如下内容:
string[] ingredients = { "Salt", "Sugar", "Pepper" };
foreach (string x in myList)
{
string ingredient = ingredients.FirstOrDefault(x.Contains); // i.e., i => x.Contains(i)
if (ingredient != null)
{
MessageBox.Show("The ingredient found in this line of myList is: " + ingredient);
}
}
如果这对于您来说太神秘了,那么请创建一个具有描述性名称的扩展方法:
// Put this in a static class somewhere.
public static string FindAny(this string s, params string[] values)
{
return values.FirstOrDefault(s.Contains);
}
并这样称呼它:
string ingredient = x.FindAny(ingredients);
// Or, at the expense of extra memory allocations, you could inline the array:
// string ingredient = x.FindAny("Salt", "Sugar", "Pepper");
if (ingredient != null)
{
MessageBox.Show("The ingredient found in this line of myList is: " + ingredient);
}
注意:要输出在同一行中找到的多种成分,请使用Where而不是FirstOrDefault:
string[] ingredients = { "Salt", "Sugar", "Pepper" };
foreach (string x in myList)
{
List<string> ingredientsFound = ingredients.Where(x.Contains).ToList();
if (ingredientsFound.Count > 0)
{
MessageBox.Show("The ingredient(s) found in this line of myList are: " +
string.Join(", ", ingredientsFound));
}
}
这是一种简单的方法。 此外,它还会打印找到与行匹配的所有成分。
我喜欢Michael的解决方案,但是知道您可以对两个数组进行简单的i
, j
迭代是必不可少的。
string[] INGREDIENTS = {"Salt", "Sugar"};
foreach (string line in myList)
{
foreach (string ingredient in INGREDIENTS)
{
if (line.Contains(ingredient))
{
MessageBox.Show($"The ingredient found in this line of myList is: {ingredient}");
}
}
}
对于更广泛的可能成分,以及相对较小的字符串(但任意数量的字符串),使用regex可以显示出更好的性能,因为它不必每次都对所有成分重复字符串。
static readonly Regex ingredientRegex = new Regex("(Salt|Sugar|Pepper)", RegexOptions.Compiled | RegexOptions.CultureInvariant);
static void FindIngredient(string[] myList)
{
foreach(string x in myList)
{
var match = ingredientRegex.Match(x);
if(match.Success)
{
Console.WriteLine("The ingredient found in this line of myList is: " + match.Groups[1].Value);
}
}
}
只要记住要事先构造Regex对象即可。
这类事情的一般解决方案是将其封装在您自己的调用函数中。 在有多种方法可以使用库函数的情况下,Michael Liu的答案可能更可取。 但是,如果没有,您将执行以下操作:
bool ContainsAny(string haystack, IEnumerable<string> needles, out which)
{
foreach(var s in needles)
{
if (haystack.Contains(s))
{
which = s;
return true;
}
}
which = null;
return false;
}
然后打电话给。
foreach (string x in myList)
{
if (x.Contains("Salt") || x.Contains("Sugar") || x.Contains("Pepper"))
{
string s = "The ingredient found in this line of myList is: {0}", x;
MessageBox.Show(s);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.