Newyearnt Newyearnt - 1 month ago 9
C# Question

Remove diarictics

I have

List<string>
with multi language

var list = new List<string>();
list.Add("menu");
list.Add("ménu");
list.Add("Hotel");
list.Add("hótel");
.....

var result = list.Contains("me");


I need get 2 items: "menu" and "ménu".

Answer

You can do this with:

var searchTerm = "me";
var outList = new List<String>();
foreach (var item in list)
{
    var formattedItem = Regex.Replace(item.Normalize(NormalizationForm.FormD), "[^A-Za-z| ]", string.Empty);
    var result = (formattedItem.Contains(searchTerm)) ? true : false;
    if (result)
    {
            outList.Add(item);
    }
}  
  • First replace all "special" characters with "standard" ones (those without accents)
  • Then check if the current word we are iterating around contains the input ("me")
  • If it does, add it to outList

Or, if you need to use Linq:

var searchTerm = "me";
var outList = new List<String>();
foreach (var item in list
    .Where(x => Regex.Replace(x.Normalize(NormalizationForm.FormD), "[^A-Za-z| ]", string.Empty)
    .Contains(searchTerm)))
{
    outList.Add(item);
}