Created
March 14, 2025 12:04
-
-
Save PiotrFerenc/a34ebc5e453eca0a82e7c396342617b8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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