2

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.

Miguel Ping
  • 18,082
  • 23
  • 88
  • 136
  • identifying typos is a very complex problem. are you going to compare every input word with every valid word? please look up spelling suggestions etc on the web – Miserable Variable Feb 11 '09 at 12:21
  • I'm not looking for input validation. I'm looking for string comparison. – Miguel Ping Feb 11 '09 at 18:49
  • 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](http://stackoverflow.com/questions/132478/how-to-perform-string-diffs-in-java). – Bill the Lizard Feb 11 '09 at 13:09

2 Answers2

8

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
  • 16,829
  • 7
  • 33
  • 31
1

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
  • 188,989
  • 46
  • 291
  • 292