asp.net找出字符串存在最多的字符与数量

来源:百度知道 编辑:UC知道 时间:2024/04/28 01:56:24
有一道题比较经典: String str="fdsahkdfsughnudfshguhdsughdisufghssjdjfg";
找出str存在最多的字符与数量

引用
using System.Text.RegularExpression;

得到结果
int getnum=Regex.Matches(sr.ToString(),"A").Count

String str = "fdsahkdfsughnudfshguhdsughdisufghssjdjfg";
char mostChar = str[0];
int mostCount = str.Split(mostChar).Length;
for (int i = 1; i < str.Length; i++)
{
int cnt = str.Split(str[i]).Length;
if (cnt > mostCount)
{
mostChar = str[i];
mostCount = cnt;
}
}

Console.WriteLine("最多的字符是 {0} : 数目是{1}", mostChar, mostCount);