Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# string comparison method returning index of first non match

Is there an exsting string comparison method that will return a value based on the first occurance of a non matching character between two strings?

i.e.

string A = "1234567890"

string B = "1234567880"

I would like to get a value back that would allow me to see that the first occurance of a matching break is A[8]

like image 443
Andy Avatar asked Dec 14 '11 12:12

Andy


4 Answers

/// <summary>
/// Gets a first different char occurence index
/// </summary>
/// <param name="a">First string</param>
/// <param name="b">Second string</param>
/// <param name="handleLengthDifference">
/// If true will return index of first occurence even strings are of different length
/// and same-length parts are equals otherwise -1
/// </param>
/// <returns>
/// Returns first difference index or -1 if no difference is found
/// </returns>
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;
}
like image 94
sll Avatar answered Nov 09 '22 13:11

sll


If you have .net 4.0 installed, this could be a way:

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

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

edit:

Though, if you need the position:

    int? firstocurrence = A.Zip(B, (p, q) => new { A = p, B = q })
            .Select((p, i) => new { A = p.A, B = p.B, idx = i })
            .Where(p => p.A != p.B)
            .Select(p => p.idx)
            .FirstOrDefault();
like image 20
Francisco Avatar answered Nov 09 '22 14:11

Francisco


An extension method along the lines of the below would do the job:

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;
}
like image 2
glosrob Avatar answered Nov 09 '22 13:11

glosrob


Not that I know of, but it's pretty trivial:

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;
}

This is a simple ordinal comparison. Ordinal case-insensitive comparison is an easy change, but culture-base is tricky to define; "Weißbier" mismatches "WEISSBIERS" on the final S in the second string, but does that count as position 8 or position 9?

like image 2
Jon Hanna Avatar answered Nov 09 '22 14:11

Jon Hanna