views:

1231

answers:

3

Hi,

I'm looking for a simple java lib/src to highlight differences between two Strings, case-sensitive. A html output would be great, but I would be happy to get the indexes of the diffs, something like:

diff("abcd","aacd") 
> [2,2]
diff("maniac", "brainiac")
> ["man",brain"] or [0,3] or something like that

The idea is to higlight typos or such in a swing program, since the input shold follow strict conventions.

+6  A: 

Apache Commons Lang has a class called StringUtils which has both difference and indexOfDifference which fulfills your needs.

http://commons.apache.org/lang/

Check it out

Mikezx6r
+2  A: 

This isn't really a duplicate, since you're asking for how to highlight the difference, but there are some good answers on this related question.

Bill the Lizard
A: 

The java-diff project might also be useful.

This is an implementation of the longest common subsequences (LCS) algorithm for Java. The Diff#diff() method returns a list of Difference objects, each of which describes an addition, a deletion, or a change between the two collections.

Michael Myers