Miguel Ping
Miguel Ping

Reputation: 18347

Java simple String diff util

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.

Upvotes: 2

Views: 6598

Answers (2)

Michael Myers
Michael Myers

Reputation: 191965

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.

Upvotes: 1

Mikezx6r
Mikezx6r

Reputation: 16905

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

Upvotes: 8

Related Questions