如何找到两个字符串之间的区别?

时间:2023-01-09 21:32:28

I have two strings and would like to display the difference between them. For example, if I have the strings "I am from Mars" and "I am from Venus", the output could be "I am from Venus". (Typically used to show what changed in an audit log, etc.)

我有两个字符串,想要显示它们之间的区别。例如,如果我有“我来自火星”和“我来自金星”的字符串,输出可能是“我来自维纳斯”。 (通常用于显示审核日志中的更改等)

Is there a simple algorithm for this? I am using C# but I guess a generic algorithm could be adapted from any programming language.

有一个简单的算法吗?我正在使用C#,但我想可以从任何编程语言改编通用算法。

Or is there a framework class/third-party library that will do this sort of thing?

或者是否有框架类/第三方库可以执行此类操作?

1 个解决方案

#1


32  

Check this out: http://en.wikipedia.org/wiki/Diff#Algorithm

看看这个:http://en.wikipedia.org/wiki/Diff#Algorithm

Also: http://en.wikipedia.org/wiki/Longest_common_subsequence_problem

There is also an implementation described here: http://www.codeproject.com/KB/recipes/DiffAlgorithmCS.aspx

此处还介绍了一种实现:http://www.codeproject.com/KB/recipes/DiffAlgorithmCS.aspx

#1


32  

Check this out: http://en.wikipedia.org/wiki/Diff#Algorithm

看看这个:http://en.wikipedia.org/wiki/Diff#Algorithm

Also: http://en.wikipedia.org/wiki/Longest_common_subsequence_problem

There is also an implementation described here: http://www.codeproject.com/KB/recipes/DiffAlgorithmCS.aspx

此处还介绍了一种实现:http://www.codeproject.com/KB/recipes/DiffAlgorithmCS.aspx