Home >Backend Development >C++ >How Can I Perform Case-Insensitive String Comparisons in C# While Ignoring Accents?

How Can I Perform Case-Insensitive String Comparisons in C# While Ignoring Accents?

Linda Hamilton
Linda HamiltonOriginal
2025-01-24 15:56:10839browse

How Can I Perform Case-Insensitive String Comparisons in C# While Ignoring Accents?

Handling Accents and Case in C# String Comparisons

C# string comparisons can be complicated by accented characters and case sensitivity. This article demonstrates how to perform case-insensitive comparisons while also ignoring diacritical marks (accents).

Accented characters are often treated differently than their non-accented counterparts, leading to inaccurate comparisons. To address this, we can preprocess strings to remove accents before comparison. The following RemoveDiacritics function uses Unicode normalization to achieve this:

<code class="language-csharp">static string RemoveDiacritics(string text)
{
    string formD = text.Normalize(NormalizationForm.FormD);
    StringBuilder sb = new StringBuilder();

    foreach (char ch in formD)
    {
        UnicodeCategory uc = CharUnicodeInfo.GetUnicodeCategory(ch);
        if (uc != UnicodeCategory.NonSpacingMark)
        {
            sb.Append(ch);
        }
    }

    return sb.ToString().Normalize(NormalizationForm.FormC);
}</code>

This function normalizes the input string and then iterates through its characters, removing any non-spacing marks (accents). The result is a string with accents removed.

Now, to perform a case-insensitive comparison ignoring accents, simply apply this function to your strings before using Equals:

<code class="language-csharp">string s1 = "résumé";
string s2 = "resume";

bool areEqual = RemoveDiacritics(s1).Equals(RemoveDiacritics(s2), StringComparison.OrdinalIgnoreCase); // true</code>

Using StringComparison.OrdinalIgnoreCase provides a culture-insensitive case-insensitive comparison, ensuring consistent results across different systems. This approach ensures that strings with and without accents are treated as equivalent when performing case-insensitive comparisons, improving the accuracy and robustness of your application logic.

The above is the detailed content of How Can I Perform Case-Insensitive String Comparisons in C# While Ignoring Accents?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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