c#字符串比较方法返回第一个非匹配的索引

是否有一个exsting字符串比较方法,它将根据两个字符串之间非匹配字符的第一次出现返回一个值?

string A = "1234567890" string B = "1234567880" 

我想得到一个值,这将使我看到匹配中断的第一次出现是A [8]

 ///  /// Gets a first different char occurence index ///  /// First string /// Second string ///  /// If true will return index of first occurence even strings are of different length /// and same-length parts are equals otherwise -1 ///  ///  /// Returns first difference index or -1 if no difference is found ///  public int GetFirstBreakIndex(string a, string b, bool handleLengthDifference) { int equalsReturnCode = -1; if (String.IsNullOrEmpty(a) || String.IsNullOrEmpty(b)) { return handleLengthDifference ? 0 : equalsReturnCode; } string longest = b.Length > a.Length ? b : a; string shorten = b.Length > a.Length ? a : b; for (int i = 0; i < shorten.Length; i++) { if (shorten[i] != longest[i]) { return i; } } // Handles cases when length is different (a="1234", b="123") // index=3 would be returned for this case // If you do not need such behaviour - just remove this if (handleLengthDifference && a.Length != b.Length) { return shorten.Length; } return equalsReturnCode; } 

下面的扩展方法可以完成这项工作:

 public static int Your_Name_Here(this string s, string other) { string first = s.Length < other.Length ? s : other; string second = s.Length > other.Length ? s : other; for (int counter = 0; counter < first.Length; counter++) { if (first[counter] != second[counter]) { return counter; } } return -1; } 

不是我所知道的,但它非常简单:

 public static int FirstUnmatchedIndex(this string x, string y) { if(x == null || y == null) throw new ArgumentNullException(); int count = x.Length; if(count > y.Length) return FirstUnmatchedIndex(y, x); if(ReferenceEquals(x, y)) return -1; for(idx = 0; idx != count; ++idx) if(x[idx] != y[idx]) return idx; return count == y.Length? -1 : count; } 

这是一个简单的序数比较。 序数不区分大小写的比较是一个很容易的变化,但文化基础很难定义; “Weißbier”在第二个字符串的最后一个S上与“WEISSBIERS”不匹配,但这是否算作位置8或位置9?

如果你安装了.net 4.0,这可能是一种方式:

  string A = "1234567890"; string B = "1234567880"; char? firstocurrence = A.Zip(B, (p, q) => new { A = p, B = q }) .Where(p => pA != pB) .Select(p => pA) .FirstOrDefault(); 

编辑:

但是,如果你需要这个职位:

  int? firstocurrence = A.Zip(B, (p, q) => new { A = p, B = q }) .Where(p => pA != pB) .Select((p, i) => i) .FirstOrDefault(); 

可以编写字符串扩展名

 public static class MyExtensions { public static IList Mismatch(this string str1, string str2) { var char1 = str1.ToCharArray(); var char2 = str2.ToCharArray(); IList Resultchar= new List(); for (int i = 0; i < char2.Length;i++ ) { if (i >= char1.Length || char1[i] != char2[i]) Resultchar.Add(char2[i]); } return Resultchar; } } 

用它就好

 var r = "1234567890".Mismatch("1234567880"); 

它不是用于找到不匹配的优化算法。

如果您只对找到第一个不匹配感兴趣,

 public static Char FirstMismatch(this string str1, string str2) { var char1 = str1.ToCharArray(); var char2 = str2.ToCharArray(); for (int i = 0; i < char2.Length;i++ ) { if (i >= char1.Length || char1[i] != char2[i]) return char2[i]; } return ''c; } 

本主题是比较字符串的副本, 并获得它们彼此不同的第一个位置 ,其中包含使用Linq的更好的一行解决方案