How to replace special characters with their equivalent (such as “ á ” for “ a”) in C#?

后端 未结 4 1452
悲&欢浪女
悲&欢浪女 2020-12-16 00:45

I need to get the Portuguese text content out of an Excel file and create an xml which is going to be used by an application that doesn\'t support characters such as \"ç\",

4条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-16 01:28

    string text = {text to replace characters in};
    
    Dictionary replacements = new Dictionary();
    
    // add your characters to the replacements dictionary, 
    // key: char to replace
    // value: replacement char
    
    replacements.Add('ç', 'c');
    ...
    
    System.Text.StringBuilder replaced = new System.Text.StringBuilder();
    for (int i = 0; i < text.Length; i++)
    {
        char character = text[i];
        if (replacements.ContainsKey(character))
        {
            replaced.Append(replacements[character]);
        }
        else
        {
            replaced.Append(character);
        }
    }
    
    // 'replaced' is now your converted text
    

提交回复
热议问题