本文介绍了获得两个字符串之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我如何区分两个字符串的内容?
例如:
string test1 =word1,word2, word3,word4;
string test2 =word2,word4;
现在我希望两个字符串之间的差异为word1,word3
HELP !!
how can i get difference between contents of two strings??
for example:
string test1 ="word1,word2,word3,word4";
string test2 ="word2,word4";
now i want difference between two strings as "word1,word3"
HELP!!
推荐答案
string test1 = "word1,word2,word3,word4";
string test2 = "word2,word4";
string result = string.Join(",", test1.Split(',').Except(test2.Split(',')));
如果你想找到test1中的内容而不是test2中的内容,还要查找test2中的内容而不是test1:
If you wanted to find what was in test1 and not in test2, but also find what was in test2 and not in test1:
string test1 = "word1,word2,word3,word4";
string test2 = "word2,word4,word5";
var lst1 = test1.Split(',');
var lst2 = test2.Split(',');
var listDistinct = lst1.Concat(lst2);
var result = string.Join(",",
listDistinct.Except(lst2)
.Concat(listDistinct.Except(lst1))
.OrderBy(x=> x));
这篇关于获得两个字符串之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!