4个回答
展开全部
这个需要靠编程,可以通过循环,每次搜索到一个red就加1.
给你参考代码:
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
label1.Text = string.Format("string count:{0}", GetCount(richTextBox1.Text));
}
private int GetCount(string str)
{
int cunt=0;
string[] strs = str.Split(new char[] { ' ' });
foreach(string tpStr in strs)
{
if(tpStr.ToLower().IndexOfAny(
new char[]{'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q',
'r','s','t','u','v','w','x','y','z'})>=0)
{
cunt++;
}
}
return cunt;
}
给你参考代码:
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
label1.Text = string.Format("string count:{0}", GetCount(richTextBox1.Text));
}
private int GetCount(string str)
{
int cunt=0;
string[] strs = str.Split(new char[] { ' ' });
foreach(string tpStr in strs)
{
if(tpStr.ToLower().IndexOfAny(
new char[]{'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q',
'r','s','t','u','v','w','x','y','z'})>=0)
{
cunt++;
}
}
return cunt;
}
展开全部
使用Regex类,指定一个正则表达式,
使用MatchCollection类 获取 Regex类实例的所有匹配,
:
string regexText = @"(red)";//需要匹配的字串
string inputs = "asdfredadfaredrearfredrererdreadfred" //目标文本;
MatchCollection mc = new Regex(regexText).Matches(inputs);
mc.Count //匹配数量.
使用MatchCollection类 获取 Regex类实例的所有匹配,
:
string regexText = @"(red)";//需要匹配的字串
string inputs = "asdfredadfaredrearfredrererdreadfred" //目标文本;
MatchCollection mc = new Regex(regexText).Matches(inputs);
mc.Count //匹配数量.
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
下面这个count就是你要的结果
int i=0;
int count = 0;
string test = "redaabredjjkreddd";
while(test.IndexOf("red",i)>=0)
{
i = test.IndexOf("red",i) + 1;
count++;
}
int i=0;
int count = 0;
string test = "redaabredjjkreddd";
while(test.IndexOf("red",i)>=0)
{
i = test.IndexOf("red",i) + 1;
count++;
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
string str="asdfnewsdafasdnewssdfnews";
string substr="red";
int count= str.Replace(substr,"|").Split('|').Length-1;
string substr="red";
int count= str.Replace(substr,"|").Split('|').Length-1;
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询