如何计算字符串中指定字符串的个数? 对于一个字符串,如:str="abcabcabcabc"如何计算其中子串abc的个数,我想了半天没想出来,请高手指点一个,最好写出算法,谢谢! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 int count = 0;int startIndex = 0;while(startIndex <str.length){ startIndex = str.IndexOf("abc", startIndex ); if(startIndex <0) break; count++;} string str = "abcabaceabc";Regex regex = new Regex( "abc" );int n = regex.Matches( str , 0 ).Count; 需要想半天!?两行代码就搞定了string str = "abcabcabcabc";string temp = str.Replace("abc","");int count = (str.Length - temp.Length) / 3;Console.WriteLine("字符串中abc的数量是:"+count); Regex reg = new Regex(@"abc"); string s = "abcabcabc"; this.TextBox1.Text = reg.Matches(s).Count.ToString(); ---------------------------------------------EMail:[email protected] 请给我一个与您交流的机会! nattystyle(霹雳冰) 的方法不错。 nattystyle(霹雳冰)的是传统方法,以前在VB6中常用的~ Ivony(授人以鱼不如授人以渔,上海谋生)过奖过奖:) 如何调用FindWindow API获取主窗体 Distinct的用法 为什么写不进数据库 以一点填充周围不规则区域 abstract 的问题 关于值类型和引用类型的思考? 关于C#中 接口的问题 windows服务进程问题 有关用C#控制FLASH的显示问题,查了92条帖子,没有我想要的,只好再问了,谢谢先 c# 串口通信serialport,循环发送问题,求大神赐教 初用水晶报表:水晶报表能动态显示dataGridView控件的数据吗? 本人新手,请问如何在C#中使用水晶报表,那里有这样的例子资料什么的
int startIndex = 0;while(startIndex <str.length)
{
startIndex = str.IndexOf("abc", startIndex );
if(startIndex <0)
break; count++;}
Regex regex = new Regex( "abc" );
int n = regex.Matches( str , 0 ).Count;
string temp = str.Replace("abc","");
int count = (str.Length - temp.Length) / 3;
Console.WriteLine("字符串中abc的数量是:"+count);
string s = "abcabcabc";
this.TextBox1.Text = reg.Matches(s).Count.ToString();
---------------------------------------------
EMail:[email protected] 请给我一个与您交流的机会!