Replace a list of invalid character with their valid version (like tr)

后端 未结 4 1080
你的背包
你的背包 2021-01-05 18:26

I need to do something like this dreamed .trReplace:

  str = str.trReplace(\"áéíüñ\",\"aeiu&\");

It should change this str

4条回答
  •  礼貌的吻别
    2021-01-05 19:11

    I did something similar for ICAO Passports. The names had to be 'transliterated'. Basically I had a Dictionary of char to char mappings.

    Dictionary mappings;
    
    static public string Translate(string s)
    {
       var t = new StringBuilder(s.Length);
       foreach (char c in s)
       {
          char to;
          if (mappings.TryGetValue(c, out to))
             t.Append(to);
          else
             t.Append(c);
        }
        return t.ToString();
     }
    

提交回复
热议问题