Jeremy Stein Jeremy Stein - 1 year ago 46
C# Question

Best way to check for string in comma-delimited list with .NET?

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"


If I check for
"apple"
I should find it, but if I check for
"app"
I should not.

What's the most straight-forward and concise way to do this? It doesn't have to be fast.

(I'll add my solution as an answer, but I'm hoping someone has something better.)

Answer Source

Using linq:

listString.Split(',').Contains("apple")

W/o linq:

Array.IndexOf(listString.Split(','), "apple") >= 0