views:

162

answers:

4

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 algorithm or library will work.

Thanks, Mark

+1  A: 

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.

Donut
+2  A: 

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.

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

Mark Byers
It's for integrating into an ASP.NET website, so needs to not be a program. Clarafied question.
MStodd
+6  A: 

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.

Aaronaught
+1 - Looks good!
Mark Byers
DiffPlex rocks.
MStodd
A: 

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.

Frenchie