Jeremy Stein Jeremy Stein - 1 year ago 60
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:


If I check for
I should find it, but if I check for
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:


W/o linq:

Array.IndexOf(listString.Split(','), "apple") >= 0
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download