How can I check if a string contains a character in C#?

后端 未结 8 987
陌清茗
陌清茗 2020-12-02 09:43

Is there a function I can apply to a string that will return true of false if a string contains a character.

I have strings with one or more character options such

8条回答
  •  猫巷女王i
    2020-12-02 10:29

    You can use the IndexOf method, which has a suitable overload for string comparison types:

    if (def.IndexOf("s", StringComparison.OrdinalIgnoreCase) >= 0) ...
    

    Also, you would not need the == true, since an if statement only expects an expression that evaluates to a bool.

提交回复
热议问题