日期:2014-05-18 浏览次数:20857 次
A="|AA|BB|CC|DD|" B="|AA|FF|" C="|BB|DD|EE|" D="|CC|" E="|BB|DD|"
string A = "|AA|BB|CC|DD|"; string B = "|AA|FF|"; string C = "|BB|DD|EE|"; string D = "|CC|"; string E = "|BB|DD|"; var result = A.Split('|').Concat(B.Split('|')).Concat(C.Split('|')).Concat(D.Split('|')).Concat(E.Split('|')).Where(a => string.IsNullOrEmpty(a) == false).GroupBy(a => a).Select(a => a.Key + "(" + a.Count() + ")"); foreach (var str in result) Console.Write(str + " "); Console.WriteLine();
------解决方案--------------------
PS:每次看到
string.IsNullOrEmpty(a) == false
这种写法都有种说不清道不明的冲动……
又来写个一行的:
string A = "|AA|BB|CC|DD|"; string B = "|AA|FF|"; string C = "|BB|DD|EE|"; string D = "|CC|"; string E = "|BB|DD|"; (A + B + C + D + E).Split('|').GroupBy(s => s).ToList().ForEach(g => Console.Write(!string.IsNullOrEmpty(g.Key) ? g.Key + "(" + g.Count() + ") " : "")); Console.WriteLine();
------解决方案--------------------
var query=Regex.Split((A+B+C+D+E).Trim('|'),"\\|+").GroupBy(s=>s).Select(g=>string.Format("{0} : {1}",g.Key,g.Count()));
------解决方案--------------------
string A = "|AA|BB|CC|DD|"; string B = "|AA|FF|"; string C = "|BB|DD|EE|"; string D = "|CC|"; string E = "|BB|DD|"; var query = (A + B + C + D + E).Replace(@"||", @"|").Trim('|').Split('|').GroupBy(word => word).Select(word => new { word = word.Key, Count = word.Count() }); query.ToList().ForEach(word => Console.WriteLine(word.word + "(" + word.Count + ")")); /* AA(2) BB(3) CC(2) DD(3) EE(1) FF(1) */
------解决方案--------------------
string A = "|AA|BB|CC|DD|"; string B = "|AA|FF|"; string C = "|BB|DD|EE|"; string D = "|CC|"; string E = "|BB|DD|"; string result = string.Empty; Dictionary<string, int> dictionary = new Dictionary<string, int>(); List<string> list = new List<string>() {A,B,C,D,E }; foreach (string item in list) { string temp_Str = item.Trim('|'); string[] string_arr = temp_Str.Split('|'); foreach (string temp_item in string_arr) { if (dictionary.ContainsKey(temp_item)) { dictionary[temp_item] = dictionary[temp_item]+1; } else { dictionary.Add(temp_item,1); } } }