C# Diff Algorithm for Text [closed]

為{幸葍}努か 提交于 2019-12-18 11:30:00

问题


I'm looking for a diff algorithm that will produce results like SO's edit revisions page. I've more or less just started looking and I'm not opposed to doing it myself but I don't need to reinvent the wheel.

I'll be using C# 4.0. I'll basically have two strings, and old one and a new one. I want to know what has changed in the new one by highlighting and strike throughs.


回答1:


Its based on Longest common subsequence algorithm, popularly known as LCS.

LCS of old text and new text gives the part that has remain unchanged. So the parts of old text that is not part of LCS is the one that got changed.

From the wiki page above:

It is a classic computer science problem, the basis of diff (a file comparison program that outputs the differences between two files), and has applications in bioinformatics.




回答2:


You can take a look at Menees Diff for an example written in C#.




回答3:


I found that Google has published code for doing diff, match and patch which contains c# classes and test-code. The code is not too hard to use IMHO.

https://code.google.com/archive/p/google-diff-match-patch/

Well documented here:

https://code.google.com/archive/p/google-diff-match-patch/wikis/API.wiki




回答4:


Usually implemented with a longest common substring algorithm. This post will be of interest.



来源:https://stackoverflow.com/questions/3764107/c-sharp-diff-algorithm-for-text

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!