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

后端 未结 9 1358
没有蜡笔的小新
没有蜡笔的小新 2021-02-02 09:37

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\"
9条回答
  •  伪装坚强ぢ
    2021-02-02 10:22

    Regex probably doesn't count as "straight-forward", but this is what I've got:

    Regex.IsMatch(listString, "(?<=,|^)" + testWord + "(?=,|$)")
    

    Update: Per Eric's comment below, this should be:

    Regex.IsMatch(listString, "(?<=,|^)" + Regex.Escape(testWord) + "(?=,|$)")
    

提交回复
热议问题