出个难题,哪位高人来解解决!! 有个字符串 str=a,b,c,a,e,f,d,a,e,a现在要统计出 这个字符串中 有多少个a,多少个b,多少个c 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 不会做,但有点思路,不知道对不对做个循环把字符串中各个数字取一遍substring(i,i+1)然后计数case a anum+=1case b bnum+=1...代码不大会写,请指教. /// <summary> /// 个数 /// </summary> /// <param name="strText">字符串</param> /// <param name="strWord">单词:a,b,c,d,</param> /// <returns></returns>public static int CountWord(string strText, string strWord) { int count = 0; if (!string.IsNullOrEmpty(strText)) { string[] strWords = strText.Split(','); for (int i = 0; i < strWords.Length; i++) { if (strWords [i] == strWord) //是否相等 { count++; } } } return count; } private string GetSameCount(string str) { int length=str.Length; string result=string.Empty; if(length==0) return result; while(length>0) { string tempChar=str[0].ToString(); string tempStr=str.Replace(tempChar,""); int tempLen=tempStr.Length; result+=(length-tempLen)+tempChar; length=tempLen; } return result; }调用时:str="a,b,c,a,e,f,d,a,e,a";string result=GetSameCount(str.Replace(",","")); string s="asdfasdf"; Dictionary<char, int> list = new Dictionary<char, int>(); foreach (char c in s.ToCharArray()) { if (list.ContainsKey(c)) { list[c] += 1; } else { list.Add(c, 1); } } 完整代码: string s="asdfasfdasedfdadfddddddf"; Dictionary<char, int> list = new Dictionary<char, int>(); foreach (char c in s.ToCharArray()) { if (list.ContainsKey(c)) { list[c] += 1; } else { list.Add(c, 1); } } foreach (KeyValuePair<char, int> item in list) { Console.WriteLine("字符{0}有{1}", item.Key, item.Value); } Console.Read(); 建立一个数组count[26],记录a-z的计数int[] count = new int[26];for(int k = 0; k < str.Length; k++){ if(char.IsLetter(str[k])) { int n = str[k] - 'A'; if(n > 26) { n = str[k] - 'a'; } count[n]++; }} 请教类似lock方法的问题 快疯了,把文件从数据库中写入指定文件。 pocekt pc 2003 如何找到指定的进程exe(立即给分) 求解 关于C#中窗体的一个小问题 XML文件读取判断的问题? Microsoft Visual Studio .NET 2003 引导程序插件 关于Server Application Unavailable问题 关于datagrid行样式的问题 asp.net里如何支持团队开发? 请问《C#高级编程》哪有下载??? 文件上传 有几个函数,只有函数名不同,如何以更简单的形式编写这几个函数,或用一个函数代替这几个函数?
做个循环把字符串中各个数字取一遍substring(i,i+1)
然后计数
case a anum+=1
case b bnum+=1
...代码不大会写,请指教.
/// <summary>
/// 个数
/// </summary>
/// <param name="strText">字符串</param>
/// <param name="strWord">单词:a,b,c,d,</param>
/// <returns></returns>
public static int CountWord(string strText, string strWord)
{
int count = 0;
if (!string.IsNullOrEmpty(strText))
{
string[] strWords = strText.Split(','); for (int i = 0; i < strWords.Length; i++)
{
if (strWords [i] == strWord) //是否相等
{
count++;
}
}
}
return count;
}
{
int length=str.Length;
string result=string.Empty;
if(length==0) return result;
while(length>0)
{
string tempChar=str[0].ToString();
string tempStr=str.Replace(tempChar,"");
int tempLen=tempStr.Length;
result+=(length-tempLen)+tempChar;
length=tempLen;
}
return result;
}调用时:
str="a,b,c,a,e,f,d,a,e,a";
string result=GetSameCount(str.Replace(",",""));
Dictionary<char, int> list = new Dictionary<char, int>();
foreach (char c in s.ToCharArray())
{
if (list.ContainsKey(c))
{
list[c] += 1;
}
else
{
list.Add(c, 1);
}
}
string s="asdfasfdasedfdadfddddddf";
Dictionary<char, int> list = new Dictionary<char, int>();
foreach (char c in s.ToCharArray())
{
if (list.ContainsKey(c))
{
list[c] += 1;
}
else
{
list.Add(c, 1);
}
}
foreach (KeyValuePair<char, int> item in list)
{
Console.WriteLine("字符{0}有{1}", item.Key, item.Value);
}
Console.Read();
for(int k = 0; k < str.Length; k++)
{
if(char.IsLetter(str[k]))
{
int n = str[k] - 'A';
if(n > 26)
{
n = str[k] - 'a';
}
count[n]++;
}
}