当前位置:编程学习 > C#/ASP.NET >>

如何替换一个string中的字节

如何替换一个string中每第五个字节 例如 把每第五个字节都换成X 输入“The Cat Sat on the Mat”, 输出是“The Xat SXt onXthe Xat”谢谢各位啦 --------------------编程问答-------------------- http://bbs.csdn.net/topics/390287270 --------------------编程问答-------------------- 循环string的每个字符,如果能被5整除,你就替换,一个个拼接 --------------------编程问答--------------------
引用 2 楼 bdmh 的回复:
循环string的每个字符,如果能被5整除,你就替换,一个个拼接


+1 --------------------编程问答-------------------- Regex reg = new Regex(@"(....)(.)");
string result = reg.Replace(yourStr, "$1X"); --------------------编程问答--------------------
string test = "The Cat Sat on the Mat";
Regex reg = new Regex(@"(?<=\G.{4}).");
string result = reg.Replace(test, "X");
--------------------编程问答-------------------- 我是现学现卖哈哈哈 --------------------编程问答--------------------
@"(.)(.)"

--------------------编程问答--------------------
引用 5 楼 lxcnn 的回复:
C# code?123string test = "The Cat Sat on the Mat";Regex reg = new Regex(@"(?<=\G.{4}).");string result = reg.Replace(test, "X");


+1 --------------------编程问答--------------------

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();
        }
--------------------编程问答-------------------- 我是现学现卖哈哈哈  --------------------编程问答--------------------

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

--------------------编程问答-------------------- 7楼邪恶了....
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,