Home >Backend Development >C++ >How to Perform a Case-Insensitive String Contains Check in C#?
C#string that does not distinguish between lower and lowercases compares
The method in
C#is used to check whether one string contains another string, but by default, it is distinguished from the case. To perform comparisons that do not distinguish between lower and lowercase, you can use the following methods:
String.Contains
<.> 1. Use
String.IndexOf
StringComparison
Using Methods and can ignore the appliances in the search process:
String.IndexOf
<.> 2. Create a custom extension method StringComparison.OrdinalIgnoreCase
<code class="language-csharp">string title = "STRING"; bool contains = title.IndexOf("string", StringComparison.OrdinalIgnoreCase) >= 0;</code>Another method is to create a custom expansion method for the string to provide the required behavior:
How to use:
<code class="language-csharp">public static class StringExtensions { public static bool Contains(this string source, string toCheck, StringComparison comp) { return source?.IndexOf(toCheck, comp) >= 0; } }</code>Note:
For the old version of C#that does not support the operating symbol (?..), Please use the following code:
<code class="language-csharp">string title = "STRING"; bool contains = title.Contains("string", StringComparison.OrdinalIgnoreCase);</code>
The above is the detailed content of How to Perform a Case-Insensitive String Contains Check in C#?. For more information, please follow other related articles on the PHP Chinese website!