Skip to content

Commit

Permalink
Create an extension function to find the index of a difference betwee…
Browse files Browse the repository at this point in the history
…n two

strings.
  • Loading branch information
Kinematics committed Dec 17, 2015
1 parent 3c3eaa6 commit 4d998f2
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions TallyCore/Utility/LinqExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -231,5 +231,31 @@ public static string AgnosticMatch(this string value, IEnumerable<string> list)
return null;
}

/// <summary>
/// Find the first character difference between two strings.
/// </summary>
/// <param name="first">First string.</param>
/// <param name="second">Second string.</param>
/// <returns>Returns the index of the first difference between the strings. -1 if they're equal.</returns>
public static int FirstDifferenceInStrings(this string first, string second)
{
if (first == null)
throw new ArgumentNullException(nameof(first));
if (second == null)
throw new ArgumentNullException(nameof(second));

int length = first.Length < second.Length ? first.Length : second.Length;

for (int i = 0; i < length; i++)
{
if (first[i] != second[i])
return i;
}

if (first.Length != second.Length)
return Math.Min(first.Length, second.Length);

return -1;
}
}
}

0 comments on commit 4d998f2

Please sign in to comment.