2

How to get a special algorithm so that the capital and simple letters have the same value. Example - Ā -> Aa
- ā -> aa
- Č -> Ch
- č -> ch

public static readonly Dictionary<string, string> dict = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
{
    {"Ā", "Aa"},
    {"Č", "Ch"},
    {"Ē", "Ee"},
    {"Ī", "Ii"},
    {"Š", "Sh"},
    {"Ū", "Uu"},
    {"Ž", "Zh"}
};

Ignore case

string inputText = Console.ReadLine();

    foreach (var item in dict)
    {
        inputText = inputText.Replace(item.Key, item.Value);
    }
  • 2
    Add the lowercase letters too – Tim Schmelter Oct 30 '18 at 09:59
  • 1
    I would first question whether you need to do this at all, there are cultural string comparison code in .NET that can handle much of this kind of complexity out of the box, as long as you tell it which culture you want it to use when comparing. Have you figured out that you actually need to do this? – Lasse V. Karlsen Oct 30 '18 at 10:00
  • 1
    As an example of what I mean, you can try this code: `StringComparer.Create(CultureInfo.GetCultureInfo("de-DE"), true).Equals("heiße", "heisse")`, which does a German text comparison where `ß` and `ss` are considered equal. Are you sure there is no culture available that will handle this? – Lasse V. Karlsen Oct 30 '18 at 10:03
  • @LasseVågsætherKarlsen: this looks more like removing diacritics, like some do for example with german umlauts(`ü` -> `ue` , `ä` -> `ae` ,...) – Tim Schmelter Oct 30 '18 at 10:08
  • I agree and I couldn't find any culture in .NET that handle this case, nor the Norwegian similar case where we handle `Å` and `Aa` as the same thing, so I guess this really is necessary. – Lasse V. Karlsen Oct 30 '18 at 10:19
  • 1
    https://stackoverflow.com/questions/249087/how-do-i-remove-diacritics-accents-from-a-string-in-net/34272324#34272324 – Drag and Drop Oct 30 '18 at 10:26

1 Answers1

3

Replace is Case sensitive, so your code will only replace those with capital letters, you can do the same replacement for small letters too:

foreach (var item in dict)
{
        inputText = inputText.Replace(item.Key, item.Value)
                             .Replace(item.Key.ToLower(), item.Value.ToLower());
}

but as @TimSchmelter said, its better to have both lowercase and uppercase letters in your dictionary like:

public static readonly Dictionary<string, string> dict = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
{
    {"Č", "Ch"},
    {"č", "ch"},
    //other items
};
Ashkan Mobayen Khiabani
  • 33,575
  • 33
  • 102
  • 171
  • 2
    To avoid culture issues i'd add the lowercase letter key-value pairs into the dictionary as well – Tim Schmelter Oct 30 '18 at 10:00
  • @TimSchmelter you are absolutely right, I got the implication that he wants to do it without writing lowercase values, thats why i answered like this, I myself would have done it the way you said. let me add it to my answer. – Ashkan Mobayen Khiabani Oct 30 '18 at 10:03