I\'m reading a comma-delimited list of strings from a config file. I need to check whether another string is in that list. For example:
\"apple,banana,cheese\"
Using linq:
listString.Split(',').Contains("apple")
W/o linq:
Array.IndexOf(listString.Split(','), "apple") >= 0
This works, but regexes are pretty slow if they get complicated. That said, the word boundary anchor makes this sort of thing pretty easy.
var foods = "apple,banana,cheese";
var match = Regex.Match(foods, @"\bapple\b");
if (match.Success)
Console.WriteLine(match.Value);
This is solution for ignoring case and culture(or select one of the property of StringComparer
)
var commaDelimitedString = "string1,string2,string3,string4";
var testString = string2.trim; // trim to remove the spaces coz you may be getting it from UI
if(commaDelimitedString.split(',').contains(testString,StringComparer.InvariantCultureIgnoreCase))
{
//TODO: What to do when condition is true
}
else
{
//TODO: What to do when condition is false
}