求 字符串中相同字符的个数。。。。。
假设随便输入字符串 adfdfkeaaaldcleidlfadtuduu
判断出 有多少个a 多少个d 等等
在C#中怎么写?
------解决方案--------------------string s = "adfdfkeaaaldcleidlfadtuduu";
Dictionary <char,int> dic = new Dictionary <char,int>();
for(int i=0;i<s.length;i++)
{
if(dic.Contains(s[i]))
{
dic[s[i]]= dic[s[i]]+1;
}
else
{
dic.Add(s[i],1);
}
}
------解决方案--------------------不会LINQ 好象LINQ一行就可以了
给你个循环的把
private void button2_Click(object sender, EventArgs e)
{
Dictionary<char, int> _Value = GetTextCount("adfdfkeaaaldcleidlfadtuduu");
}
public Dictionary<char, int> GetTextCount(string p_Text)
{
Dictionary<char, int> _Re = new Dictionary<char, int>();
while (true)
{
int _Value =p_Text.Length;
char _CharKey = p_Text[0];
p_Text = p_Text.Replace(_CharKey.ToString(), "");
_Re.Add(_CharKey, _Value - p_Text.Length);
if (p_Text.Length == 0) return _Re;
}
}
------解决方案--------------------思路:
1.取字符串长度1,
2.取第一个字符x,
3.将字串中的x全部替换成空,将值赋给原串
4.再取字串长度2
5.两个长度之差就是x的个数
6.循环,直到字串为空.
------解决方案--------------------修改了string s = "adfdfkeaaaldcleidlfadtuduu";
Dictionary <char,int> dic = new Dictionary <char,int>();
for(int i=0;i <s.length;i++)
{
if(dic.ContainsKey(s[i]))
{
dic[s[i]]= dic[s[i]]+1;
}
else
{
dic.Add(s[i],1);
}
}
------解决方案--------------------正则表达式。。判断匹配次数。。。汗
------解决方案--------------------
------解决方案--------------------C# code
string str = "adfdfkeaaaldcleidlfadtuduu ";
Regex re = new Regex(@"a");
MatchCollection mac = re.Matches(str);
int a =0;
foreach (Match m in mac)
{
a = Regex.Matches(str, m.Value).Count;
//Console.WriteLine(m.Value);
}
Console.WriteLine(a);
------解决方案--------------------
不是很了解C#,学习中,不过我觉得这个应该很容易实现
------解决方案--------------------
C# code
string str = "adfdfkeaaaldcleidlfadtuduu ";
Regex reg = new Regex(@"a", RegexOptions.Singleline);
Console.WriteLine(reg.Matches(str).Count);
------解决方案--------------------
using System;
using System.Collections;
class SetArray
{
static public Hashtable TestStr(string str)
{
char[] a = str.ToCharArray();
Hashtable table = new Hashtable();
for (int i = 0; i < a.Length; i++)
{
if (str.Substring(0, i).IndexOf(a[i]) == -1)
{
int count = 0;
foreach (char x in a)
{
if (x == a[i])
{
count++;
}
}