0
string[] myArray= { "replay", "answer" };
if (myArray.Contains("rểplay")) {
//...
}

This function will return false since it try to check diacritics word "rểplay" instead of "replay". How can I ignore nonspacing combining characters, such as diacritics and return true?

And how can it work for vice versa like below?

string[] myArray= { "rểplay", "answer" };
if (myArray.Contains("replay")) {
//...
}

And how to applied in this function also?

var ix = Array.FindIndex(myKeys, p => p.Equals(wordIn, StringComparison.CurrentCultureIgnoreCase));
wordOut = myKeys[ix];
return true;
user1437001
  • 53
  • 1
  • 5
  • 1
    See http://stackoverflow.com/questions/249087/how-do-i-remove-diacritics-accents-from-a-string-in-net – L.B Jul 20 '12 at 07:02

1 Answers1

2

I would rather normalize the search string and search in the array.

using System.Globalization;

string input = "rểplay";
string decomposed = input.Normalize(NormalizationForm.FormD);
char[] filtered = decomposed
    .Where(c => char.GetUnicodeCategory(c) != UnicodeCategory.NonSpacingMark)
    .ToArray();
string newString = new String(filtered);

string[] myArray= { "replay", "answer" };
if (myArray.Contains(newString)) {
//...
}
Vinayak Kolagi
  • 1,831
  • 1
  • 13
  • 26
  • Doesn't look like this should work. You didn't strip diacritics. – CodesInChaos Jul 20 '12 at 07:20
  • How if vice versa? string[] myArray= { "rểplay", "answer" }; if (myArray.Contains("replay")) { //... } – user1437001 Jul 20 '12 at 07:22
  • Regex! Regex! `newString = Regex.Replace(decomposed, @"\p{Mn}", string.Empty)` – xanatos Jul 20 '12 at 07:51
  • @xanatos IMO this code is much easier to read than your regex. – CodesInChaos Jul 20 '12 at 07:55
  • @CodesInChaos Regex rarely are a solution to a problem, more often a new problem in itself :-) :-) – xanatos Jul 20 '12 at 08:01
  • @All, but it won't work for vice versa, string[] myArray= { "rểplay", "answer" }; if (myArray.Contains("replay")) { //... } – user1437001 Jul 20 '12 at 08:28
  • Is there no such of this thing "CompareOptions.IgnoreNonSpace" for checking array contains? It's just compare 2 string automatically – user1437001 Jul 20 '12 at 08:30
  • @user1437001 replacing diacritics with ascii is shown in the code. You need to normalize even the string array before comparison using the same method. Otherwise, if that is not the option probably you need to look for fuzzy string matching to find more similar word and its index. – Vinayak Kolagi Jul 20 '12 at 08:54