views:

50

answers:

2

Does a library or even standard API call exist that allows me to diff two strings and get the number of diff chars as an int? I wouldn't mind other features, just as long as I can get a more programmatic diff result (such as an int) instead of something that just outputs the entire human readable diff.

+4  A: 

I think what you want is the Leveshtein distance - this tells you how many changes (insertions, deletions or replacements) are required to transform one string to another.

For example, the difference between abcde and abcdef is 1, because you insert f after the last position in abcde to get abcdef.

The difference between abcde and abcdf is also 1, since you replace e in the first string with f to get the second.

The difference between abcde and abde is 1 because you delete c in the first string to get the second.

Here is an implementation in Java.

danben
+1  A: 

I don't know of any standard API calls, but you could see this question for references to third-party libraries (not surprising - Google, Apache Commons ...)

How to perform string Diffs in Java?
http://stackoverflow.com/questions/132478/how-to-perform-string-diffs-in-java

Bert F
Ah, I see a StringUtils.difference(str1, str2).length() and StringUtils.difference(str2, str1).length() Should work just fine. Thanks.
Zombies