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!
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?