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.
Share
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
abcdeandabcdefis 1, because you insertfafter the last position inabcdeto getabcdef.The difference between
abcdeandabcdfis also 1, since you replaceein the first string withfto get the second.The difference between
abcdeandabdeis 1 because you deletecin the first string to get the second.A very good implementation can be found in Apache Commons Text: LevenshteinDistance.
Here are some sample implementation in Java.