通过C#编程:有字符串cccbeebbb,通过编码生成:c3b1e2b3,请写一个方法实现其编码?求教各位大侠!

解决方案 »

  1.   

      class Program
        {
            static void Main(string[] args)
            {
                string test = "aaa,中国,人民,bbb,中国,服务,中国,aaa,ccc";
                foreach (StringTimes st in GetStringAndTimes(test))
                {
                    Console.WriteLine(st.timers + "," + st.value);
                }            //foreach (StringTimes st in OrderByTimers(GetStringAndTimes(test))) 
                //{ 
                //    Console.WriteLine(st.value + "-出现次数" + st.timers.ToString() + ";"); 
                //}             Console.WriteLine("根据出现次数排序后:");
                foreach (StringTimes st in OrderByTimers(GetStringAndTimes(test)))
                {
                    Console.Write(st.value + " ");
                }
                Console.ReadKey();
            }        //对StringTimes进行排序 
            static List<StringTimes> OrderByTimers(List<StringTimes> ST)
            {
                List<StringTimes> st = ST;
                for (int i = 0; i < st.Count; i++)
                {
                    int currentTimes = st[i].timers;
                    for (int j = i; j < st.Count; j++)
                    {
                        if (st[j].timers > currentTimes)
                        {
                            StringTimes tempInt = st[i];
                            st[i] = st[j];
                            st[j] = tempInt;
                            currentTimes = st[j].timers;
                        }
                        else
                        {
                            continue;
                        }
                    }
                }            return st;
            }        static List<StringTimes> GetStringAndTimes(string Str)
            {
                List<StringTimes> timesAndString = new List<StringTimes>();            string[] strArray = Str.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);            foreach (string str in strArray)
                {
                    bool hasShow = false;
                    foreach (StringTimes st in timesAndString)
                    {
                        if (str == st.value)
                        {
                            st.timers++;
                            hasShow = true;
                            break;
                        }
                        else
                        {
                            continue;
                        }
                    }
                    if (!hasShow)
                        timesAndString.Add(new StringTimes(1, str));
                }            return timesAndString;
            }        class StringTimes
            {
                public int timers;
                public string value;            public StringTimes(int Timers, string Value)
                {
                    this.timers = Timers;
                    this.value = Value;
                }
            }    }
      

  2.   

    static void Main(string[] args)
            {
                string str = "cccbeebbb";            char[] charStr = str.ToCharArray();            string afterStr = "";            int count = 1;            for (int i = 0; i < charStr.Length; i++)
                {
                    if (i == charStr.Length - 1)
                    {
                        afterStr += charStr[i] + count.ToString();
                    }
                    else
                    {
                        if (charStr[i] != charStr[i + 1])
                        {
                            afterStr += charStr[i] + count.ToString();
                            count = 1;
                        }
                        else
                        {
                            count++;
                        }
                    }
                }            Console.WriteLine(afterStr);
                Console.ReadKey();
            }
      

  3.   

    建个CLASS,里边两个属性,CHAR NAME,INT COUNTLIST<CLASS>遍历字符串,SUBSTRING,取1位长,如果一致在上一个实例里边的COUNT+1如果不一致新建实例然后ADD到LIST里边最后输出字符串遍历LIST里边T的属性就可以了!比楼上的兄台少不了几行代码,你这问题得去算法版找牛人去!不然你转成ASCII捅咕捅咕?
      

  4.   

    恩  其实你不用新建实例,只要改变前一实例的属性,然后ADD就可以了,相同的话就覆盖LIST里边的T就行了!
      

  5.   

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;namespace TestCount
    {
        class Program
        {
            static void Main(string[] args)
            {
                string targetString = "cccbeebbb";
                StringBuilder sb = new StringBuilder();
                List<string> resultList = new List<string>();
                for (int i = 0; i < targetString.Length; i++)
                {
                    var result = from target in targetString
                                 where target == targetString[i]
                                 select target;
                    if (result.Count() > 1)
                    {
                        if (!resultList.Contains(targetString[i].ToString()))
                        {
                            resultList.Add(targetString[i].ToString());
                            resultList.Add(result.Count().ToString());
                        }
                    }
                }
                foreach (string item in resultList)
                {
                    sb.Append(item);
                }
                Console.WriteLine(sb.ToString());
                Console.ReadLine();
            }
        }
    }
    哈哈用LINQ的方法不过貌似不能达到你相同字母的计数还分开来的效果,明天在想下回复你哈哈~
      

  6.   

    (?is)([a-z])\1*匹配一下就可以了,长度就是每个匹配结果字符串的长度
      

  7.   

    都搞这个复杂?string result = Regex.Replace("cccbdddeee", @"(\w)\1*", delegate(Match m)
                                                             {
                                                             return m.Groups[1].Value + m.Groups[0].Length
                                                             });