比较两个字符串或对象,并得到差别回来 [英] Comparing two strings or objects and getting the difference back

查看:138
本文介绍了比较两个字符串或对象,并得到差别回来的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的网站上实现一种版本历史,我需要一种比较字符串或对象键的方法。例如:

I would like to implement a type of version history on my website and I need a way of comparing strings or object keys. For example:


原始字符串/对象键:更快的棕色狐狸

Original string / object key: The quicker brown fox

修改的字符串/对象键:快速棕色狐狸
跳过懒惰兔子

Revised string / object key: The quick brown fox jumped over the lazy rabbit

修订:添加跳过懒惰兔子

我只想将历史记录表中的修订版本保存。我真的不知道从哪里开始,所以任何想法如何让我去或建议的方法将非常感激。

I would like to save only the revision in my history table. I don't really know where to start, so any ideas how to get me going or advice on the approach would be really appreciated.

我知道 find()函数,我怀疑它是使用的首选,但是我不知道如何把它看成一个解决方案,因为它比较了字符串批发这样的话。

I am aware of the find() function and I suspect it's a prime candidate for use, but I don't know how to visualize it as a solution since it compares strings "wholesale" so to speak.

推荐答案

你想要一个差异算法(我已经标记的问题,这样),我强烈建议您尝试写自己。我试过 - 失败了,因为它是一个NP完整的问题,不容易包装你的头脑。相反,请查看 diff-match-patch ,它为客户端提供了JavaScript和Java实现(演示)或服务器端处理。如果您需要进行HTML区分,请查看 daisydiff ,尽管预先警告HTML / XML差异是一个痛苦的体验由于某些原因)。

You want a diffing algorithm (I've tagged the question as such), which I highly recommend you not try to write yourself. I've tried - and failed - as it's a NP complete problem and not easy to wrap your mind around. Instead, check out diff-match-patch, which has a JavaScript and Java implementation for client (demo) or server side processing. If you need to do HTML differencing look at daisydiff instead, albeit be forewarned HTML/XML diffing is truly a painful experience (see this page for some reasons why).

差异的最大可能是 GNU差异 ,其中还有 Java实现(查找GNU Diff for Java)。这种算法比diff-match-patch(dmp)更优化,虽然dmp似乎在所有时间都在改进,所以如果你需要比较非常大的字符串(例如兆字节),GNU算法可能是一个更好的赌注。

Probably the grand-daddy of diffing is GNU diff, which also has a Java implementation (find "GNU Diff for Java"). This algorithm is more optimized than diff-match-patch (dmp), albeit dmp seems to be improving all the time, so if you need to compare very large strings (e.g. megabytes) the GNU algorithm is probably a better bet.

这篇关于比较两个字符串或对象,并得到差别回来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆