如何替换一个string中每第五个字节 例如 把每第五个字节都换成X 输入“The Cat Sat on the Mat”, 输出是“The Xat SXt onXthe Xat”谢谢各位啦

解决方案 »

  1.   

    http://bbs.csdn.net/topics/390287270
      

  2.   

    循环string的每个字符,如果能被5整除,你就替换,一个个拼接
      

  3.   

    Regex reg = new Regex(@"(....)(.)");
    string result = reg.Replace(yourStr, "$1X");
      

  4.   


    string test = "The Cat Sat on the Mat";
    Regex reg = new Regex(@"(?<=\G.{4}).");
    string result = reg.Replace(test, "X");
      

  5.   


    static void Main(string[] args)
            {            string a = "The Cat Sat on the Mat";
                string b = "";
                 
                for (int i = 1; i < a.Length; i++)
                {                if (i % 5 == 0)
                    {
                        b = b + "X";
                    }
                    else
                    {
                        b = b + a[i - 1];
                    }            }
                Console.WriteLine(b);
                Console.ReadKey();
            }
      

  6.   


    string str="The Cat Sat on the Mat";
                str = Regex.Replace(str, @"([\s\S]{4})[\s\S]", "$1X");