0

I had written a code where i want to compare 2 same names but one is with diacritics

eg. Dăn will match with Dan

I want to be able to match Vietnamese names with and without diacritics but my code keep returns not matching for the checking. Is there any other way to do this?

    static void Main(string[] args)
    {
        string name1 = "Dan";
        string name2 = "Dăn";

        bool match = CompareStrings(name1, name2);

        if (match)
        {
            Console.WriteLine("The strings match.");
        }
        else
        {
            Console.WriteLine("The strings do not match.");
        }
    }

    static bool CompareStrings(string str1, string str2)
    {
        if (str1.Equals(str2, StringComparison.InvariantCultureIgnoreCase))
        {
            return true;
        }

        string str1Normalized = str1.Normalize(NormalizationForm.FormC);
        string str2Normalized = str2.Normalize(NormalizationForm.FormC);

        if (str1Normalized.Equals(str2Normalized, StringComparison.InvariantCultureIgnoreCase))
        {
            return true;
        }

        return false;
    }
Chew Hong Yu
  • 137
  • 2
  • 8
  • 1
    probably related: https://stackoverflow.com/a/3288164 – rene Apr 25 '23 at 05:46
  • 1
    There is an extremely simple solution in the duplicate (not the accepted answer): String.Compare(name1, name2, System.Globalization.CultureInfo.CurrentCulture, System.Globalization.CompareOptions.IgnoreNonSpace | System.Globalization.CompareOptions.IgnoreCase) == 0. – Palle Due Apr 25 '23 at 07:57
  • Yes, @PalleDue that indeed works for my question, thanks alot! – Chew Hong Yu Apr 26 '23 at 02:36

0 Answers0