开发者

Is there anything available for .NET to get the differences in two strings?

开发者 https://www.devze.com 2022-12-28 07:20 出处:网络
I\'m looking for a way to determine the differences between two strings, and highlight them in both strings.

I'm looking for a way to determine the differences between two strings, and highlight them in both strings.

I would suspect that most 'diff' libraries won't work since they show differences in lines (I believe).

Either an 开发者_StackOverflow社区algorithm or library will work.

Thanks, Mark


DiffPlex can handle many different kinds of "intra-line" diffs, including character and word diffs. I think it should be able to do everything you're asking for here.


From your question, you seem to have rejected using an existing program and decided to write your own because you believe existing programs cannot show differences within lines.

However WinMerge can show intra-line diffs.

Is there anything available for .NET to get the differences in two strings?

Does that meet your needs? Or do you need this to be a .NET component for some reason?


You'll probably want to look into using the Levenshtein distance, or some similar algorithm. For a C# implementation of the Levenshtein algorithm, see here (if you're really keen on writing this yourself).

This question asks something similar, with the accepted answer pointing to a bunch of diff related projects. There's a lot of good code that's been written that's definitely worth taking a look into.


String.Compare would work. If you want to compare words then just split the initial string into an array of strings and loop through it.

0

精彩评论

暂无评论...
验证码 换一张
取 消