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.

3
  • 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 Commented Feb 11, 2009 at 12:21
  • 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. Commented Feb 11, 2009 at 13:09
  • I'm not looking for input validation. I'm looking for string comparison. Commented Feb 11, 2009 at 18:49

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.