[英]How to use string.Contains for list of values
I have the following xmlnode (string) whose value would be of few given type ie: 我有以下xmlnode(字符串),其值很少给定类型,即:
"Ansi_nulls","Encrypted","Quoted_identifer" etc.
I want to test the xmlnode using xmlNode.Contains ("xxx","yyy")
.. 我想使用xmlNode.Contains xmlNode.Contains ("xxx","yyy")
来测试xmlnode。
What is the correct syntax ? 什么是正确的语法?
If you are testing the full (complete) value of the node, you can do it by reversing the call; 如果要测试节点的完整(完整)值,可以通过反转调用来完成; check that a list of known values contains your current node value: 检查已知值列表是否包含当前节点值:
new[]{"Ansi_nulls","Encrypted","Quoted_identifer", ...}.Contains(xmlNode);
Contains takes a string to test for. Contains需要一个字符串来测试。
One way to solve your problem would be to use a simple regular expression 解决问题的一种方法是使用简单的正则表达式
using System.Text.RegularExpressions;
if (Regex.IsMatch(nodevalue, "(Ansi_nulls|Encrypted|Quoted_identifer)")...
if (new[] {"xxx","yyy"}.Any(n => xmlNode.Contains(n)))
I would create an extension method using sehes solution: 我将使用sehes解决方案创建一个扩展方法:
public static bool Contains(this string source, params string[] values)
{
return values.Any(value => source.Contains(value));
}
That way you can call: 那样你就可以打电话:
xmlNode.Contains("string", "something else");
A simple if statement will work: 一个简单的if语句将起作用:
if (xmlNode.Contains("xxx") && xmlNode.Contains("yyy"))
{
// your work
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.