Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections;
using System.IO;
namespace FindNumber
{
class Program
{
//方法1
static void Method_1(string s)
{
Console.WriteLine("Method_1");
//定义一个字典变量
Dictionary<char, int> dict = new Dictionary<char, int>();
for (int i = 0; i < s.Length; i++)
{
//检查该字典中是否有该键值
if (dict.ContainsKey(s[i]))
{
dict[s[i]] = dict[s[i]] + 1;
}
else
{
dict.Add(s[i],1);
}
}
//printf
foreach(KeyValuePair<Char, int> dd in dict)
{
Console.WriteLine("{0}:{1}",dd.Key,dd.Value);
}
}
//方法2
static void Method_2(string s)
{
Console.WriteLine("Method_2");
//正则表达式变量
Regex reg = new Regex(@"a");
//要匹配的字符串
MatchCollection mac = reg.Matches(s);
int a = 0;
foreach (Match m in mac)
{
a = Regex.Matches(s,m.Value).Count;
}
Console.WriteLine(a);
}
//方法3
static void Method_3(string s)
{
Console.WriteLine("Method_3");
Regex reg = new Regex(@"a",RegexOptions.Singleline);
Console.WriteLine(reg.Matches(s).Count);
}
//方法4
static void MEthod_4(string s)
{
Console.WriteLine("Method_4");
char[] a = s.ToCharArray();
Hashtable table = new Hashtable();
for (int i = 0; i < a.Length; i++)
{
if (s.Substring(0, i).IndexOf(a[i]) == -1)//这个什么用啊???a[i]第一次出现的位置,避免重复计算.ok!!
{
int count = 0;
foreach (char x in a)
{
if (x == a[i])
{
count++;
}
} table.Add(a[i], count);
}
}
//输出
foreach (object o in table.Keys)
{
char b = (char)o;
Console.WriteLine("{0}--{1}",b,table[b].ToString());
}
}
//方法5
static void Method_5(string s)
{
Console.WriteLine("Method_5");
int cnt = 0;
//
while ((cnt = s.Length) != 0)
{
string sub = s.Substring(0,1);
s = s.Replace(sub,"");
cnt -= s.Length;
Console.WriteLine(sub+"--"+cnt.ToString());
}
}
//方法6
static void Method_6(string s)
{
Console.WriteLine("Method_6");
var queryStr = from item in s
group item by item;
string str = "";
foreach (var ss in queryStr)
{
str += ss.Key + "--" + ss.Count()+"\n";
}
//输出
Console.WriteLine(str);
}
//方法7
static void Method_7(string s)
{
Console.WriteLine("Method_7");
var query = from c in s.ToCharArray()
group c by c into dic
select dic;
foreach (var iter in query)
{
Console.WriteLine(iter.Key+"--"+iter.Count());
}
}
//方法8
static void Method_8(string s)
{
Console.WriteLine("MEthod_8");
int cnt = 0;
while ((cnt = s.Length) != 0)
{
string sub = s.Substring(0,1);
s = s.Replace(sub,"");
cnt -= s.Length;
Console.WriteLine(sub+"--"+cnt.ToString());
}
}
//方法9
static void Method_9(string s)
{
Console.WriteLine("Method_9");
string a = "";
int count = 0;
int strLength = s.Length;
while (strLength > 0)
{
a = s.Substring(0,1);
s = s.Replace(a,"");
count = strLength - s.Length;
strLength = s.Length;
Console.WriteLine(a+"--"+count);
}
}
//方法10
static void Method_10(string s)
{
Console.WriteLine("Method_10");
(from c in "adfdfkeaaaldcleidlfadtuduu"
group c by c into g
select string.Format("'{0}': {1}", g.Key, g.Count())).ToList().ForEach(Console.WriteLine);
}
static void Main(string[] args)
{
Console.WriteLine("Enter the string :");
string s = Console.ReadLine();
Method_1(s);
Method_2(s);
Method_3(s);
MEthod_4(s);
Method_5(s);
Method_6(s);
Method_7(s);
Method_8(s);
Method_9(s);
Method_10(s);
Console.ReadKey();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections;
using System.IO;
namespace FindNumber
{
class Program
{
//方法1
static void Method_1(string s)
{
Console.WriteLine("Method_1");
//定义一个字典变量
Dictionary<char, int> dict = new Dictionary<char, int>();
for (int i = 0; i < s.Length; i++)
{
//检查该字典中是否有该键值
if (dict.ContainsKey(s[i]))
{
dict[s[i]] = dict[s[i]] + 1;
}
else
{
dict.Add(s[i],1);
}
}
//printf
foreach(KeyValuePair<Char, int> dd in dict)
{
Console.WriteLine("{0}:{1}",dd.Key,dd.Value);
}
}
//方法2
static void Method_2(string s)
{
Console.WriteLine("Method_2");
//正则表达式变量
Regex reg = new Regex(@"a");
//要匹配的字符串
MatchCollection mac = reg.Matches(s);
int a = 0;
foreach (Match m in mac)
{
a = Regex.Matches(s,m.Value).Count;
}
Console.WriteLine(a);
}
//方法3
static void Method_3(string s)
{
Console.WriteLine("Method_3");
Regex reg = new Regex(@"a",RegexOptions.Singleline);
Console.WriteLine(reg.Matches(s).Count);
}
//方法4
static void MEthod_4(string s)
{
Console.WriteLine("Method_4");
char[] a = s.ToCharArray();
Hashtable table = new Hashtable();
for (int i = 0; i < a.Length; i++)
{
if (s.Substring(0, i).IndexOf(a[i]) == -1)//这个什么用啊???a[i]第一次出现的位置,避免重复计算.ok!!
{
int count = 0;
foreach (char x in a)
{
if (x == a[i])
{
count++;
}
} table.Add(a[i], count);
}
}
//输出
foreach (object o in table.Keys)
{
char b = (char)o;
Console.WriteLine("{0}--{1}",b,table[b].ToString());
}
}
//方法5
static void Method_5(string s)
{
Console.WriteLine("Method_5");
int cnt = 0;
//
while ((cnt = s.Length) != 0)
{
string sub = s.Substring(0,1);
s = s.Replace(sub,"");
cnt -= s.Length;
Console.WriteLine(sub+"--"+cnt.ToString());
}
}
//方法6
static void Method_6(string s)
{
Console.WriteLine("Method_6");
var queryStr = from item in s
group item by item;
string str = "";
foreach (var ss in queryStr)
{
str += ss.Key + "--" + ss.Count()+"\n";
}
//输出
Console.WriteLine(str);
}
//方法7
static void Method_7(string s)
{
Console.WriteLine("Method_7");
var query = from c in s.ToCharArray()
group c by c into dic
select dic;
foreach (var iter in query)
{
Console.WriteLine(iter.Key+"--"+iter.Count());
}
}
//方法8
static void Method_8(string s)
{
Console.WriteLine("MEthod_8");
int cnt = 0;
while ((cnt = s.Length) != 0)
{
string sub = s.Substring(0,1);
s = s.Replace(sub,"");
cnt -= s.Length;
Console.WriteLine(sub+"--"+cnt.ToString());
}
}
//方法9
static void Method_9(string s)
{
Console.WriteLine("Method_9");
string a = "";
int count = 0;
int strLength = s.Length;
while (strLength > 0)
{
a = s.Substring(0,1);
s = s.Replace(a,"");
count = strLength - s.Length;
strLength = s.Length;
Console.WriteLine(a+"--"+count);
}
}
//方法10
static void Method_10(string s)
{
Console.WriteLine("Method_10");
(from c in "adfdfkeaaaldcleidlfadtuduu"
group c by c into g
select string.Format("'{0}': {1}", g.Key, g.Count())).ToList().ForEach(Console.WriteLine);
}
static void Main(string[] args)
{
Console.WriteLine("Enter the string :");
string s = Console.ReadLine();
Method_1(s);
Method_2(s);
Method_3(s);
MEthod_4(s);
Method_5(s);
Method_6(s);
Method_7(s);
Method_8(s);
Method_9(s);
Method_10(s);
Console.ReadKey();
}
}
}