日期:2014-05-18 浏览次数:20908 次
string result = string.Join(" ", Regex.Split(a, @"\s+").Union(Regex.Split(b, @"\s+")).Union(Regex.Split(c, @"\s+")).Distinct());//A B C D E F
------解决方案--------------------
linq完爆
class Program { static void Main(string[] args) { var strA = "A B C"; var strB = "A D E"; var strC = "B C F"; var aList = strA.Split(' ').ToList(); var bList = strB.Split(' ').ToList(); var cList = strC.Split(' ').ToList(); aList.AddRange(bList); aList.AddRange(cList); var result = aList.Distinct(); foreach (var temp in result) { Console.WriteLine(temp); } Console.ReadLine(); } }
------解决方案--------------------
这样更优雅
string a = "A B C"; string b = "A D E"; string c = "B C F"; string d = string.Join(" ",(a + b + c).ToCharArray().Distinct().OrderBy(x => x).Select(x => x.ToString()).ToArray()).Trim();
------解决方案--------------------
比较蛋疼的方法
string d = String.Join(" ", a , b.Trim(a.ToArray()) , c.Trim((a + b).ToCharArray()));
------解决方案--------------------
http://blog.csdn.net/sheshou2/article/details/4766639
------解决方案--------------------
private void Form1_Load(object sender, EventArgs e) { string a = "A B C"; string b = "A D E"; string c = "B C F"; IList<string> chars = new List<string>(); this.FillList(chars, a); this.FillList(chars, b); this.FillList(chars, c); string result = string.Empty; foreach (string s in chars) { result += s + " "; } MessageBox.Show(result); } private void FillList(IList<string> list, string s) { string[] values = s.Split(' '); foreach (string c in values) { if (!list.Contains(c)) { list.Add(c); } } }