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.
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.
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