比较两个字符串或对象并获取差异

发布于 2024-10-10 00:01:05 字数 462 浏览 0 评论 0原文

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

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

修改后的字符串/对象键:敏捷的棕色狐狸 跳过了懒兔子

修改:添加跳过懒兔子删除

我只想在历史表中保存修订。我真的不知道从哪里开始,所以任何如何让我继续前进的想法或有关该方法的建议将非常感激。

我知道 find() 函数并且我怀疑它是使用的主要候选者,但我不知道如何将其可视化为解决方案,因为可以说它比较字符串“批发”。

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

Revision: added jumped over the lazy rabbit removed er

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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

毁梦 2024-10-17 00:01:05

您想要一个比较算法(我已将问题标记为这样),我强烈建议您不要尝试自己编写。我尝试过,但失败了,因为这是一个 NP 完全问题,不容易理解。相反,请查看 diff-match-patch,它具有 JavaScript客户端(demo)或服务器端的 Java 实现加工。如果您需要进行 HTML 差异,请查看 daisydiff,尽管预先警告 HTML/XML 差异是确实是一次痛苦的经历(出于某些原因请查看此页面)。

diffing 的鼻祖可能是 GNU diff,它也有一个 Java 实现(查找“GNU Diff for Java”)。该算法比 diff-match-patch (dmp) 更优化,尽管 dmp 似乎一直在改进,因此如果您需要比较非常大的字符串(例如兆字节),GNU 算法可能是更好的选择。

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).

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.

素罗衫 2024-10-17 00:01:05

好的,那么这个怎么样?不确定它是否按照您的意愿处理普通的旧字符串,但它似乎解决了您对不知道如何处理 Java 集成位的担忧(因为它已经编写了)。至少应该指出你的写作方向。

OK, what about this then? Not sure if it does plain old strings as you'd like, but it seems to address your concerns about not knowing how to tackle the Java integration bits (since it's already written). Should at least point you in the write direction.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文