1

I am reading a file for school and I am trying to ignore accents. I used CultureInfo but it doesn't work for me is there another way?? (example .... Clémentine = clementine)

    public static void SearchName()
    {
        string lineIn = String.Empty;
        string[] BoatInfo = new string[5];
        Console.WriteLine();

        FileStream fs = new FileStream(@"FrenchMF.txt", FileMode.Open, FileAccess.Read);
        StreamReader inputStream = new StreamReader(fs);
        lineIn = inputStream.ReadLine();

        string input = String.Empty;
        Console.WriteLine();

        Console.Write("Enter Vessel Name :");
        input = Console.ReadLine().ToLower().Trim();

        string CheckInput = String.Empty;



        while(lineIn != null)
        {
            BoatInfo = lineIn.Split(',');
            CheckInput = BoatInfo[0].ToLower();

            if (input.Equals(CheckInput)) 
            {
                Console.WriteLine("its a Match" );

            }
            else
            {
                Console.WriteLine("No Match Found!! ");
                return; 


            }      
        }
    }
  • so basically you want to replace _é_ with normal _e_? – styx Apr 25 '18 at 09:29
  • 1
    Possible duplicate of [How do I remove diacritics (accents) from a string in .NET?](https://stackoverflow.com/questions/249087/how-do-i-remove-diacritics-accents-from-a-string-in-net) – PaulF Apr 25 '18 at 09:31
  • also other ones ù ï ò ë – beginnersLuck.css Apr 25 '18 at 09:31
  • Possible duplicate of [Ignoring accented letters in string comparison](https://stackoverflow.com/questions/359827/ignoring-accented-letters-in-string-comparison) – Stefan Apr 25 '18 at 09:35
  • Just as a note: This might not be appropriate for all languages. For example in German "ü" or "ä" or "ö" would actually be translated as "ue" "ae" "oe". So if you just remove the umlauts then it would actually be an incorrect spelling. – jason.kaisersmith Apr 25 '18 at 10:57

2 Answers2

1

You can create an extension method for string:

public static string RemoveDiacritics(this string @this) {
    var normalizedString = @this.Normalize(System.Text.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);
}

And then you can say input.RemoveDiacritics().

In order for the extension method to work, you must put it in a static class:

public static class ScorableExtensions {

    public static string RemoveDiacritics(this string @this) {
        //the one above
    }
}
Alex
  • 130
  • 10
0
string.Compare(s1, s2, CultureInfo.CurrentCulture, CompareOptions.IgnoreNonSpace);

See this answer

Of course, you can create your own CultureInfo - you do not have to use the CurrentCulture:

string.Compare(s1, s2, new CultureInfo("fr-FR"), CompareOptions.IgnoreNonSpace);
Marek Fekete
  • 641
  • 3
  • 16