C# 如何将一个内容里的某些字母转换成另一种字母

比如有一篇英文文章,我想把文章内容里的26个字母里的每一个字母替代另一种字母。
比如:
英文内容:Try Firefox with the bookmarks, history and passwords from another browser.
这里的t字母换a,rs,.....以此类推 文章所有字母一次性替换另一种自定义的字母。

阅读 848
评论 2018-05-26 提问
    2 个回答
    RECTOR
    • 605

    思路是有的,将字符串转换成字符数组,再定义一个字母表的字典,字典的key为替换前的字符char,字典的value为替换后的字符char,给你写了一个简单的实现,运行效果如图:

    图片描述

    示例代码:

    static void Main(string[] args)
            {
                var str = "Try Firefox with the bookmarks, history and passwords from another browser.";
                var strArr = str.ToArray();
                var result = Converter(strArr);
                Console.WriteLine($"初始字符串:{str}");
                Console.WriteLine($"替换后的字符串:{string.Join("", result)}");
                Console.ReadKey();
            }
    
            public static char[] Converter(char[] originalCharArray)
            {
                var dict = new Dictionary<char, char> {
                    { 'a','s'},
                    { 'b','t'},
                    {'c','u'},
                    {'d','v'},
                    {'e','w'},
                    {'f','x'},
                    {'g','y'},
                    {'h','z'},
                    {'i','a'},
                    {'j','b'},
                    {'k','c'},
                    {'l','d'},
                    {'m','e'},
                    {'n','f'},
                    {'o','g'},
                    {'p','h'},
                    {'q','i'},
                    {'r','j'},
                    {'s','k'},
                    {'t','l'},
                    {'u','m'},
                    {'v','n'},
                    {'w','o'},
                    {'x','p'},
                    {'y','q'},
                    {'z','r'},
                };
                var result = new List<char>();
                for (int i = 0; i < originalCharArray.Length; i++)
                {
                    var c = originalCharArray[i];
                    var isUpperCase = Char.IsUpper(c);
                    if (Char.IsLetter(c))
                    {
                        c = dict[Char.ToLower(c)];
                        if (isUpperCase)
                        {
                            c = Char.ToUpper(c);
                        }
                    }
                    result.Add(c);
                }
                return result.ToArray();
            }
    评论 赞赏 2018-05-28

      正则 + LINQ

      string str = "Try Firefox with the bookmarks, history and passwords from another browser.";
      //            Tjq Fajwxgp oalz lzw tggcesjck, zaklgjq sfv hskkogjvk xjge sfglzwj tjgokwj.
      var dict = new Dictionary<string, string> {
          {"a","s"},{"b","t"},{"c","u"},{"d","v"},{"e","w"},{"f","x"},{"g","y"},
          {"h","z"},{"i","a"},{"j","b"},{"k","c"},{"l","d"},{"m","e"},{"n","f"},
          {"o","g"},{"p","h"},{"q","i"},
          {"r","j"},{"s","k"},{"t","l"},{"y","q"},{"z","r"}
      };
                  
      var regex  = new System.Text.RegularExpressions.Regex(String.Join("|", dict.Keys));
      var result = regex.Replace(str, m => dict[m.Value]); 
      评论 赞赏 2018-05-28
        撰写回答

        登录后参与交流、获取后续更新提醒