How to use string.Contains for list of values
I have the following xmlnode (string) whose value would be of few given type i.e:
"Ansi_nulls","Encrypted","Quoted_identifer" etc.
开发者_开发百科
I want to test the xmlnode using xmlNode.Contains ("xxx","yyy")
..
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);
I would create an extension method using sehes solution:
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");
Contains takes a string to test for.
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)))
A simple if statement will work:
if (xmlNode.Contains("xxx") && xmlNode.Contains("yyy"))
{
// your work
}
精彩评论