开发者

Java: String compare library that returns diff count as an int?

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.

A very good implementation can be found in Apache Commons Text: LevenshteinDistance.

Here are some sample 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?
How to perform string Diffs in Java?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜