[Solved] In C#, String .Replace wont replace danish chars


Most Unicode characters have multiple versions that can look very alike. For example: 1????1①⑴

var s = "æӕ".Replace("æ", "ae"); // s = "aeæ"
var v = "æӕ".Select(c => (int)c).ToArray(); // { 230, 1237 }

I consider it a good practice to expect the unexpected (especially when it comes to user input)

var s = "æӕ";
var a = s.ToCharArray(); // or use StringBuilder for non 1 to 1 character replacements

for (int i = 0; i < s.Length; i++)
    if (a[i] > 127)
        switch (a[i]) {
            case 'æ': case 'ӕ': 
                a[i] = 'e'; break;
            default: 
                Debug.Print("Unexpected character " + a[i]);
        }

s = new string(a);

Here are some unrelated Falsehoods Programmers Believe

0

solved In C#, String .Replace wont replace danish chars