对于一个字符串,如:str="abcabcabcabc"
如何计算其中子串abc的个数,
我想了半天没想出来,请高手指点一个,
最好写出算法,
谢谢!

解决方案 »

  1.   

    int count = 0;
    int startIndex = 0;while(startIndex <str.length)
    {
        startIndex =   str.IndexOf("abc", startIndex );
        
        if(startIndex <0)
                   break;     count++;}
      

  2.   

    string str = "abcabaceabc";
    Regex regex = new Regex( "abc" );
    int n = regex.Matches( str , 0 ).Count;
      

  3.   

    需要想半天!?两行代码就搞定了string str = "abcabcabcabc";
    string temp = str.Replace("abc","");
    int count = (str.Length - temp.Length) / 3;
    Console.WriteLine("字符串中abc的数量是:"+count);
      

  4.   

    Regex reg = new Regex(@"abc");
                string s = "abcabcabc";
                this.TextBox1.Text = reg.Matches(s).Count.ToString();
      
    ---------------------------------------------
    EMail:[email protected] 请给我一个与您交流的机会!
      

  5.   

    nattystyle(霹雳冰) 的方法不错。
      

  6.   

    nattystyle(霹雳冰)的是传统方法,以前在VB6中常用的~
      

  7.   

    Ivony(授人以鱼不如授人以渔,上海谋生)过奖过奖:)