以前在做这块时,找了些代码,都没有这方面的功能,最后终于得自己写,懒不成,只有去参考了一些网上接近的例子写了一个,刚找了一下,这块网上也没有人发布过类似功能。
效果图:
核心代码(部分抄别人现成的,自己再改改):
public class Difference
{
public struct Item
{
public int StartA;
public int StartB;
public int deletedA;
public int insertedB;
}
private struct SMSRD
{
internal int x, y;
}
public Item[] DiffText(string TextA, string TextB)
{
return (DiffText(TextA, TextB, false, false, false));
}
public static Item[] DiffText(string TextA, string TextB, bool trimSpace, bool ignoreSpace, bool ignoreCase)
{
Hashtable h = new Hashtable(TextA.Length + TextB.Length);
DiffData DataA = new DiffData(DiffCodes(TextA, h, trimSpace, ignoreSpace, ignoreCase));
DiffData DataB = new DiffData(DiffCodes(TextB, h, trimSpace, ignoreSpace, ignoreCase));
h = null; // free up hashtable memory (maybe)
LCS(DataA, 0, DataA.Length, DataB, 0, DataB.Length);
return CreateDiffs(DataA, DataB);
}
public static Item[] DiffInt(int[] ArrayA, int[] ArrayB)
{
DiffData DataA = new DiffData(ArrayA);
DiffData DataB = new DiffData(ArrayB);
LCS(DataA, 0, DataA.Length, DataB, 0, DataB.Length);
return CreateDiffs(DataA, DataB);
}
private static int[] DiffCodes(string aText, Hashtable h, bool trimSpace, bool ignoreSpace, bool ignoreCase)
{
string[] Lines;
int[] Codes;
int lastUsedCode = h.Count;
object aCode;
string s;
aText = aText.Replace("