Home > Backend Development > C++ > How to Perform a Case-Insensitive String Contains Check in C#?

How to Perform a Case-Insensitive String Contains Check in C#?

Susan Sarandon
Release: 2025-02-02 19:02:11
Original
397 people have browsed it

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

and

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;
Copy after login
Another method is to create a custom expansion method for the string to provide the required behavior:

<方法> How to use:

public static class StringExtensions
{
    public static bool Contains(this string source, string toCheck, StringComparison comp)
    {
        return source?.IndexOf(toCheck, comp) >= 0;
    }
}
Copy after login
<:> Note:

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);
Copy after login

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template