אני יודע שיש כבר דברים מוכנים כגון זה:
https://github.com/mmanela/diffplex/
אבל לשם ההתלמדות חשבתי לעשות אולי משהו כזה בעצמי
הייתי שמח לקבל משוב (ההשואה יוצרת טקסט html שמסמן את השינויים)
אגב אם מישהו מכיר תוכנה כבר מוכנה (לא אתר) אשמח לשמוע עליה
להלן הקוד שלי
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TextComparer
{
public static class TextCompare
{
public static void Compare(ref string text1, ref string text2)
{
text1 = $"<span style=\"background-color: lightblue;\">{text1}</span>";
text2 = $"<span style=\"background-color: lightblue;\">{text2}</span>";
// Get the LSS lss
string lssString = "placeholdertext";
List<string> lssList = new List<string>();
string copyOfText1 = text1;
string copyOfText2 = text2;
while (!string.IsNullOrEmpty(lssString))
{
try
{
lssString = FindLongestSimilarSubstring(copyOfText1.ToCharArray(), copyOfText2.ToCharArray());
if (lssString.Length > 1) { lssList.Add(lssString); }
copyOfText2 = copyOfText2.Replace(lssString, "");
copyOfText1 = copyOfText1.Replace(lssString, "");
}
catch { break; }
}
foreach (var item in lssList)
{
text1.Replace(item, $"<span style=\"background-color: white;\"{item}");
text2.Replace(item, $"<span style=\"background-color: white;\"{item}");
}
}
static string FindLongestSimilarSubstring(char[] arr1, char[] arr2)
{
// Initialize variables to keep track of the longest common substring
int[,] table = new int[arr1.Length + 1, arr2.Length + 1];
int maxLength = 0;
int endIndex = 0;
// Fill the table
for (int i = 1; i <= arr1.Length; i++)
{
for (int j = 1; j <= arr2.Length; j++)
{
if (arr1[i - 1] == arr2[j - 1])
{
table[i, j] = table[i - 1, j - 1] + 1;
if (table[i, j] > maxLength)
{
maxLength = table[i, j];
endIndex = i - 1;
}
}
else
{
table[i, j] = 0;
}
}
}
// Extract the longest common substring
if (maxLength == 0)
{
return ""; // No common substring found
}
else
{
return new string(arr1, endIndex - maxLength + 1, maxLength);
}
}
}
}