In the field of string operation, the commonly used
method provides a direct method to determine whether a string exists in another string. However, in some cases, it is essential to not distinguish the size.
String.Contains
The initial problem seeks a method that does not consider the string of the lowercase string. Although the solution that originally converted both string into uppercase may look awkward, it can use a more comprehensive method.
Use the powerful features of StringComparison
Enthusiastically provide an elegant solution for the comparison of the lower case. By passing as a parameter to the method, the expected results can be achieved.
StringComparison
Use the expansion method expansion tool set StringComparison.OrdinalIgnoreCase
IndexOf
string title = "ASTRINGTOTEST"; bool contains = title.IndexOf("string", StringComparison.OrdinalIgnoreCase) >= 0;
<方法> How to use
The extension method can be easily used to perform string comparisons that do not distinguish between lower and lower cases.
public static class StringExtensions { public static bool Contains(this string source, string toCheck, StringComparison comp) { return source?.IndexOf(toCheck, comp) >= 0; } }
By adopting these technologies, developers can satisfy applications that need to be flexible and do not distinguish between writing string comparison and effectively expand their tool sets to achieve efficient and detailed text operations.
The above is the detailed content of How Can I Perform Case-Insensitive String Comparisons in C#?. For more information, please follow other related articles on the PHP Chinese website!