Skip to content

Instantly share code, notes, and snippets.

@PiotrFerenc
Created March 14, 2025 12:04
Show Gist options
  • Save PiotrFerenc/a34ebc5e453eca0a82e7c396342617b8 to your computer and use it in GitHub Desktop.
Save PiotrFerenc/a34ebc5e453eca0a82e7c396342617b8 to your computer and use it in GitHub Desktop.
public static string GetDifference(string baseString, string comparedString)
{
if (baseString == null || comparedString == null)
throw new ArgumentNullException("Input strings cannot be null.");
if (comparedString.StartsWith(baseString))
{
// Jeśli comparedString zaczyna się od baseString, zwróć różnicę
return comparedString.Substring(baseString.Length);
}
// Szukanie różnicy bardziej ogólnie (jeśli różnice mogą być też w środku)
int minLength = Math.Min(baseString.Length, comparedString.Length);
int diffIndex = 0;
// Znajdź pierwsze miejsce, gdzie stringi się różnią
for (int i = 0; i < minLength; i++)
{
if (baseString[i] != comparedString[i])
{
diffIndex = i;
break;
}
// Jeśli są identyczne do końca mniejszego stringa
if (i == minLength - 1)
{
diffIndex = minLength;
}
}
// Zwracamy różnicę od momentu rozjazdu
return comparedString.Substring(diffIndex);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment