如何获取String.Replace()替换的个数?

解决方案 »

  1.   


                string str = "aafsdfsdfdasfaf";
                int count = str.Length - str.Replace("a", "").Length;
      

  2.   

    string s="aafsdfsdfdasfaf";
    var count = (from c in s where c=='a' select c).Count();
    s=s.Replace("a","*");在Replace之前,先算出你要替换的字符的数量
      

  3.   


    int c = str.split(new string[]{"aaaa"},StringSplitOptions.None).Count-1;
      

  4.   


    长度不一定是几位,但Replace时替换的内容肯定是固定的了,根据长度差计算一下就是了
    string src = "aabbccaaddee";
    string replaceStr = "aa";
    int count = (src.Length - src.Replace(replaceStr, "").Length) / replaceStr.Length;
    richTextBox2.Text = count.ToString();
    长度为0的容错处理自己做
      

  5.   

    linq to objects只能找char?
    string.Split()是一个办法……多谢了……虽然我觉得效率不太高……或者可以使用string.IndexOf()然后再手动逐个替换?问题解决,结贴散分。