C#识别比较2个字符串的不同字符
C# Identify the different character in comparison of 2 strings
var str1 = "ASD_ZIHJK2A";
\\ 输出:对 1 个不同的字符:Z
var str2 = "ASD_YIHJK2A";
\\ 输出:对 1 个不同的字符:Y
var str3 = "AWD_PIHJK2A";
\\ 输出:对 2 个不同的字符:P
var str4 = "AWD_IHJK2AQ";
\\ 输出:对 2 个不同的字符:Q
var str5 = "ASD_RTHJK2A";
\\ 输出:对 3 个不同的字符:J
var str6 = "ASD_RTHIK2A";
\输出:对 3 差:I
使用 C# 查找对和对中每个字符串的状态差异的最简单方法是什么?
-比较的字符串长度相等
-字符串也包含介于两者之间的数字
-“不同字符”在第二个字符串中的位置可以不同
接近 Linq.Except
其中
produces the set difference of two sequences.
The set difference of two sets is defined as the members of the first set that don't appear in the second set.
This method returns those elements in first that don't appear in second. It doesn't return those elements in second that don't appear in first. Only unique elements are returned.
var strOne = "ASD_ZIHJK";
var strTwo = "ASD_YIHJK";
var difference_strOne = strOne.Except(strTwo); // Z
var difference_strTwo = strTwo.Except(strOne); // Y
直接的解决方案
public static char? FindFirstDifference(string first, string second)
{
int length = Math.Min(first.Length, second.Length);
for (int = 0; i < length; i++)
{
if (first[i] != second[i])
{
return first[i];
}
}
return null;
}
var str1 = "ASD_ZIHJK2A";
\\ 输出:对 1 个不同的字符:Z
var str2 = "ASD_YIHJK2A";
\\ 输出:对 1 个不同的字符:Y
var str3 = "AWD_PIHJK2A";
\\ 输出:对 2 个不同的字符:P
var str4 = "AWD_IHJK2AQ";
\\ 输出:对 2 个不同的字符:Q
var str5 = "ASD_RTHJK2A";
\\ 输出:对 3 个不同的字符:J
var str6 = "ASD_RTHIK2A";
\输出:对 3 差:I
使用 C# 查找对和对中每个字符串的状态差异的最简单方法是什么?
-比较的字符串长度相等
-字符串也包含介于两者之间的数字
-“不同字符”在第二个字符串中的位置可以不同
接近 Linq.Except
其中
produces the set difference of two sequences. The set difference of two sets is defined as the members of the first set that don't appear in the second set.
This method returns those elements in first that don't appear in second. It doesn't return those elements in second that don't appear in first. Only unique elements are returned.
var strOne = "ASD_ZIHJK";
var strTwo = "ASD_YIHJK";
var difference_strOne = strOne.Except(strTwo); // Z
var difference_strTwo = strTwo.Except(strOne); // Y
直接的解决方案
public static char? FindFirstDifference(string first, string second)
{
int length = Math.Min(first.Length, second.Length);
for (int = 0; i < length; i++)
{
if (first[i] != second[i])
{
return first[i];
}
}
return null;
}