C#如何判断某一特定字符在字符串中出现的次数?例子如下

比如判断下面字符串有多少个“@”字符
1@3@4@5@6@7@8@!23*35*67*34*56*78*45*68*89*45*67*09*46*78*90*67*87*78*89*34*67*

用Linq来统计指定字符的数量

using System;
using System.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string m = "1@3@4@5@6@7@8@!23*35*67*34*56*78*45*68*89*45*67*09*46*78*90*67*87*78*89*34*67*";
            Console.WriteLine(m);
            
            Console.Write("输入要搜索的字符:");
            char s = Convert.ToChar(Console.ReadLine());

            var qry = from c in m
                      where
                          c == s
                      select c;
            Console.WriteLine("字符串中{0}的数量为{1}", s, qry.Count());
        }
    }
}

温馨提示:内容为网友见解,仅供参考
第1个回答  2015-06-21
已经知道要判断的字符,那就好办了。

int count = 0;
for(int i = 0; i< s.Length; i++) //s是你的字符串数组
{
if(s[i]=="@")

{
count++;

}
}
相似回答