(1)
static void Main(string[] args) { string a = "abcdefgabc"; int i = a.Length; while (i > 0) { Console.WriteLine(a[0].ToString() + ":"); a = a.Replace(a[0].ToString(), ""); Console.Write(i - a.Length); Console.WriteLine(); i = a.Length; } }
(2)
static void Main(string[] args) { Console.WriteLine("请输入字符串:"); string str = Console.ReadLine(); CharCount(str); } public static void CharCount(string str) { if (str == string.Empty) return; char ch = str.Substring(0).ToCharArray()[0]; int num = str.Split(ch).Length - 1; Console.WriteLine("char: {0} num: {1}", ch, num); CharCount(str.Replace(ch.ToString(), "")); }
(3)
static void Main(string[] args) { string s = "abcdefgabc"; Dictionary<char, int> list = new Dictionary<char, int>(); for (int i = 0; i < s.Length; i++) { if (list.ContainsKey(s[i])) { list[s[i]]++; } else { list.Add(s[i], 1); } } foreach (char key in list.Keys) { Console.WriteLine("{0}:{1}", key, list[key]); } }
(4)
static void Main() { calculateChar("wshuguo"); } //求算一任意长度字符串中不同的字符以及它的个数 public static void calculateChar(string anyStr) { var query = from letter in anyStr group letter by letter into g select new { letter = g.Key, count = g.Count() }; query.ToList().ForEach(q => Console.WriteLine("letter:{0},count:{1}", q.letter, q.count)); }
6988

被折叠的 条评论
为什么被折叠?



