假设随便输入字符串 adfdfkeaaaldcleidlfadtuduu 判断出 有多少个a 多少个d 等等

ContractedBlock.gifExpandedBlockStart.gifCode
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<charint> dict = new Dictionary<charint>();
            
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();
        }
    }
}

转载于:https://www.cnblogs.com/binlyzhuo/archive/2009/06/06/1497675.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值