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
string title = "STRING"; bool contains = title.IndexOf("string", StringComparison.OrdinalIgnoreCase) >= 0;
<方法> How to use:
public static class StringExtensions { public static bool Contains(this string source, string toCheck, StringComparison comp) { return source?.IndexOf(toCheck, comp) >= 0; } }
For the old version of C#that does not support the operating symbol (?..), Please use the following code:
string title = "STRING"; bool contains = title.Contains("string", StringComparison.OrdinalIgnoreCase);
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!