Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display word differences using c#?

Tags:

c#

diff

word-diff

I would like to show differences between two blocks of text. Rather than comparing lines of text or individual characters, I would like to just compare words separated by specified characters ('\n', ' ', '\t' for example). My main reasoning for this is that the block of text that I'll be comparing generally doesn't have many line breaks in it and letter comparisons can be hard to follow.

I've come across the following O(ND) logic in C# for comparing lines and characters, but I'm sort of at a loss for how to modify it to compare words.

In addition, I would like to keep track of the separators between words and make sure they're included with the diff. So if a space is replaced by a hard return, I would like that to come up as a diff.

I'm using Asp.Net (c#) to display the entire block of text including the deleted original text and added new text (both will be highlighted to show that they were deleted/added). A solution that works with those technologies would be appreciated.

Any advice for how to accomplish this is appreciated.

like image 245
Jim Geurts Avatar asked Dec 19 '09 21:12

Jim Geurts


1 Answers

Microsoft has released a diff project on CodePlex that allows you to do word, character, and line diffs. It is licensed under Microsoft Public License (Ms-PL).

https://github.com/mmanela/diffplex

like image 139
Jim Geurts Avatar answered Oct 06 '22 18:10

Jim Geurts