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.)
To check if value exists in a comma separated list, you can use FIND_IN_SET() function. Now you can insert some records in the table using insert command. Display all records from the table using select statement.
To check if a string str contains specified character value , or say if specified character is present in the string, use C# String. Contains(Char) method. Call Contains() method on the string str and pass the character value as argument. Contains() method returns True if str contains value .
Using linq:
listString.Split(',').Contains("apple")
W/o linq:
Array.IndexOf(listString.Split(','), "apple") >= 0
(","+listString+",").Contains(","+testWord+",");
but not straight-forward, too.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With