0

I am new to Visual Studio and i'm trying to learn some simple tasks. I have been given a code that compares two strings (last name and first name)

    private bool compareNames(String value1, String value2)
    {
        if (value1 != null && value2 != null && value1.Trim().ToLower(). Equals(value2.Trim().ToLower()))
        {
            return true;
        }

        return false;
    }

The code above ignores case sensitive, but what i'm trying to do is to also ignore special characters like ăîşéááö.

I've tried to do this task with Normalize() but it doesnt seem to work.

    private bool compareNames(String value1, String value2)
    {
        if (value1 != null && value2 != null && value1.Trim().ToLower(). Equals(value2.Trim().ToLower()))
        {
            return true;
        }
        else if (value1 != null && value2 != null && value1.Trim().Normalize().Equals(value2.Trim().Normalize()))
        {
            return true;
        }

        return false;
    }

Any help is appreciated!

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
asphy1
  • 27
  • 5
  • Did you try the solution here - http://stackoverflow.com/questions/20674577/how-to-compare-unicode-characters-that-look-alike? – Wiktor Stribiżew Oct 12 '16 at 09:15
  • Thanks for your reply. I've tried it this way else if (value1 != null && value2 != null && value1.Trim().Normalize(NormalizationForm.FormKD).Equals(value2.Trim().Normalize(NormalizationForm.FormKD))) { return true; } But it doesnt seem to work either. – asphy1 Oct 12 '16 at 09:24
  • I mean the RemoveDiacritics approach. – Wiktor Stribiżew Oct 12 '16 at 09:24
  • Oh, thanks, missed that part. I've read few similar posts and ended up with the solution above. But it worked with RemoveDiacritics approach. – asphy1 Oct 12 '16 at 09:50

1 Answers1

0

One of the possible answers is to use the RemoveDiacritcs approach.

static string RemoveDiacritics(string text) 
{
    var normalizedString = text.Normalize(NormalizationForm.FormD);
    var stringBuilder = new StringBuilder();

    foreach (var c in normalizedString)
    {
        var unicodeCategory = CharUnicodeInfo.GetUnicodeCategory(c);
        if (unicodeCategory != UnicodeCategory.NonSpacingMark)
        {
            stringBuilder.Append(c);
        }
    }

    return stringBuilder.ToString().Normalize(NormalizationForm.FormC);
}

More info here : How do I remove diacritics (accents) from a string in .NET?

Community
  • 1
  • 1
Valentin Michalak
  • 2,089
  • 1
  • 14
  • 27