C# 在两个字符串之间查找不常见的字符
我有以下代码:C# 在两个字符串之间查找不常见的字符,c#,c#-4.0,C#,C# 4.0,我有以下代码: public static void Main (string[] args) { string word1 = "AN"; string word2 = "ANN"; //First try: var intersect = word1.Intersect(word2); var unCommon1 = word1.Except(intersect).Union(word2.Except(intersect)); //Sec
public static void Main (string[] args) {
string word1 = "AN";
string word2 = "ANN";
//First try:
var intersect = word1.Intersect(word2);
var unCommon1 = word1.Except(intersect).Union(word2.Except(intersect));
//Second try:
var unCommon = word1.Except(word2).Union(word2.Except(word1));
}
我试图得到的结果是N
。我尝试了几种方法通过阅读在线帖子来获得它,但我无法理解。是否有一种方法可以使用linq在两个字符串之间获取不常见的字符
字符串中字符的顺序并不重要。
这里还有几个场景:
FOO&BAR将导致F、O、O、B、A、R。
ANN&NAN将导致空字符串。这里是一个直接的LINQ函数
string word1 = "AN";
string word2 = "ANN";
//get all the characters in both strings
var group = string.Concat(word1, word2)
//remove duplicates
.Distinct()
//count the times each character appears in word1 and word2, find the
//difference, and repeat the character difference times
.SelectMany(i => Enumerable.Repeat(i, Math.Abs(
word1.Count(j => j == i) -
word2.Count(j => j == i))));
此代码的结果是什么?“FOO”和“BAR”的结果是什么?对于“ANN”和“NAN”?@CodeCaster上面的代码给出了空字符串。FOO和BAR将生成F、O、O、B、A、R。ANN和NAN将生成空字符串。你的问题需要包括这些额外的细节。那么顺序不重要了?@CodeCaster没有顺序就不重要了matter@CodeCaster-最新问题。