利用C#编程计算某个字符在某个字符串中出现的次数
2015-03-26 17:56
351 查看
一、利用Replace(效率不高)
代码1:
string test = "good good study day day up";
string r = test.Replace("oo", "");
int num = (test.Length - r.Length)/2;
Console.WriteLine(num);
test = "good good study day day up";
r = test.Replace("o", "");
num = (test.Length - r.Length);
Console.WriteLine(num);
二、利用Split(效率最低)
test = "good good study day day up";
int i = test.Split('d').Length - 1;
代码3:
test = "good good study day day up";
int a = test.Split(new string[] { "oo" }, StringSplitOptions.None).Length - 1;
三、利用循环(效率高)
int c2 = 0;
for (int i = 0; i < test .Length; i++)
{
if (test [i] == 'a')
{
c2++;
}
}
四、利用正则表达式
Regex rege = new Regex("o", RegexOptions.Compiled);
int count = rege.Matches(test).Count;
五、高效查找字符串出现次数
string str = "fecvcsdwfchkeov[page]ove283673ewekl[page]fsdh5d37op"; //被查的字符串
int count = 0; //计数器
string search = "[page]"; //要查的字符串
for (int i = 0; i < str.Length - search.Length; i++)
{
if (str.Substring(i, search.Length) == search)
{
count++;
}
}
六、IndexOf、While查找
string text = "今天下雪了吗,明天不会下雪了吧,什么时候才不下雪啊,我要去上学啊!";
string keyWord = "下雪";
int index = 0;
int count = 0;
while ((index = text.IndexOf(keyWord,index)) != -1)
{
count++;
Console.WriteLine("第{0}次;索引是{1}", count, index);
index = index + keyWord.Length;
}
Console.WriteLine("下雪出现的总次数:{0}", count);
七、foreach
int count;
string str="abcbabcbabbcabbabcccc";
foreach(char s in str)
{
if(s=="a")
{
count++;
}
}
console.write(count.tostring());
代码1:
string test = "good good study day day up";
string r = test.Replace("oo", "");
int num = (test.Length - r.Length)/2;
Console.WriteLine(num);
test = "good good study day day up";
r = test.Replace("o", "");
num = (test.Length - r.Length);
Console.WriteLine(num);
二、利用Split(效率最低)
test = "good good study day day up";
int i = test.Split('d').Length - 1;
代码3:
test = "good good study day day up";
int a = test.Split(new string[] { "oo" }, StringSplitOptions.None).Length - 1;
三、利用循环(效率高)
int c2 = 0;
for (int i = 0; i < test .Length; i++)
{
if (test [i] == 'a')
{
c2++;
}
}
四、利用正则表达式
Regex rege = new Regex("o", RegexOptions.Compiled);
int count = rege.Matches(test).Count;
五、高效查找字符串出现次数
string str = "fecvcsdwfchkeov[page]ove283673ewekl[page]fsdh5d37op"; //被查的字符串
int count = 0; //计数器
string search = "[page]"; //要查的字符串
for (int i = 0; i < str.Length - search.Length; i++)
{
if (str.Substring(i, search.Length) == search)
{
count++;
}
}
六、IndexOf、While查找
string text = "今天下雪了吗,明天不会下雪了吧,什么时候才不下雪啊,我要去上学啊!";
string keyWord = "下雪";
int index = 0;
int count = 0;
while ((index = text.IndexOf(keyWord,index)) != -1)
{
count++;
Console.WriteLine("第{0}次;索引是{1}", count, index);
index = index + keyWord.Length;
}
Console.WriteLine("下雪出现的总次数:{0}", count);
七、foreach
int count;
string str="abcbabcbabbcabbabcccc";
foreach(char s in str)
{
if(s=="a")
{
count++;
}
}
console.write(count.tostring());
相关文章推荐
- C#计算当前字符在字符串出现的次数
- 还是利用Map特性,计算字符串里字符出现的次数(Javascript版-Obj特性)
- C# 计算字符串中每个字符出现的次数
- 计算字符在字符串是出现的次数
- 计算字符在字符串中出现的次数
- php中计算未知长度的字符串哪个字符出现的次数最多的代码
- ORACLE计算字符在字符串中出现的次数
- 去除一个字符串中出现的重复字符,并计算出出现最多次的字符的出现次数
- 计算字符串中每种字符出现的次数(用Dictionary实现)
- 黑马程序员之C#学习笔记:求一个字符串中各个字符出现的次数
- 计算字符串中出现字符的次数
- 易车网的一道面试题:JS计算字符串中出现次数最多的字符和出现的次数
- C#计算字符串中子串出现次数的另类方法
- sqlserver分隔字符串,查找父类下所有子类,删除重复字符串,计算一字符串在别一字符中出现的次数
- 计算字符串中每种字符出现的次数(面试题)
- 黑马程序员-计算字符串中每种字符出现的次数
- 计算字符串中每种字符出现的次数
- C# 在一个字符串中,找出出现次数最多的字母,并计算次数?
- C#String.IndexOf检索字符串中字符出现的次数
- JavaScript计算字符串中每个字符出现的次数