How to display word differences using c#?

Jim Geurts picture Jim Geurts · Dec 19, 2009 · Viewed 10.6k times · Source

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.

Answer

Jim Geurts picture Jim Geurts · Jan 12, 2010

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