Class StringCompareUtil
- Since:
- 2020/07/17
- Version:
- 1.0
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic intgetFuzzyDistance(CharSequence term, CharSequence query, Locale locale) Find the Fuzzy Distance which indicates the similarity score between two Strings.static intSource: https://github.com/apache/commons-lang/blob/954338a1522aa32b869966be63ef833d10cdd79e/src/main/java/org/apache/commons/lang3/StringUtils.java#L2262-L2350static intindexOfDifference(CharSequence cs1, CharSequence cs2) Source: https://github.com/apache/commons-lang/blob/954338a1522aa32b869966be63ef833d10cdd79e/src/main/java/org/apache/commons/lang3/StringUtils.java#L3021-L3063
-
Constructor Details
-
StringCompareUtil
public StringCompareUtil()
-
-
Method Details
-
indexOfDifference
Source: https://github.com/apache/commons-lang/blob/954338a1522aa32b869966be63ef833d10cdd79e/src/main/java/org/apache/commons/lang3/StringUtils.java#L3021-L3063Compares two CharSequences, and returns the index at which the CharSequences begin to differ.
For example,
indexOfDifference("i am a machine", "i am a robot") -> 7StringUtils.indexOfDifference(null, null) = -1 StringUtils.indexOfDifference("", "") = -1 StringUtils.indexOfDifference("", "abc") = 0 StringUtils.indexOfDifference("abc", "") = 0 StringUtils.indexOfDifference("abc", "abc") = -1 StringUtils.indexOfDifference("ab", "abxyz") = 2 StringUtils.indexOfDifference("abcde", "abxyz") = 2 StringUtils.indexOfDifference("abcde", "xyz") = 0- Parameters:
cs1- the first CharSequence, may be nullcs2- the second CharSequence, may be null- Returns:
- the index where cs1 and cs2 begin to differ; -1 if they are equal
-
getFuzzyDistance
public static int getFuzzyDistance(@Nonnull CharSequence term, @Nonnull CharSequence query, @Nonnull Locale locale) Find the Fuzzy Distance which indicates the similarity score between two Strings.
This string matching algorithm is similar to the algorithms of editors such as Sublime Text, TextMate, Atom and others. One point is given for every matched character. Subsequent matches yield two bonus points. A higher score indicates a higher similarity.
StringUtils.getFuzzyDistance(null, null, null) = IllegalArgumentException StringUtils.getFuzzyDistance("", "", Locale.ENGLISH) = 0 StringUtils.getFuzzyDistance("Workshop", "b", Locale.ENGLISH) = 0 StringUtils.getFuzzyDistance("Room", "o", Locale.ENGLISH) = 1 StringUtils.getFuzzyDistance("Workshop", "w", Locale.ENGLISH) = 1 StringUtils.getFuzzyDistance("Workshop", "ws", Locale.ENGLISH) = 2 StringUtils.getFuzzyDistance("Workshop", "wo", Locale.ENGLISH) = 4 StringUtils.getFuzzyDistance("Apache Software Foundation", "asf", Locale.ENGLISH) = 3- Parameters:
term- a full term that should be matched against, must not be nullquery- the query that will be matched against a term, must not be nulllocale- This string matching logic is case insensitive. A locale is necessary to normalize both Strings to lower case.- Returns:
- result score
- Throws:
IllegalArgumentException- if either String inputnullor Locale inputnull- Since:
- 3.4
-
getLevenshteinDistance
Source: https://github.com/apache/commons-lang/blob/954338a1522aa32b869966be63ef833d10cdd79e/src/main/java/org/apache/commons/lang3/StringUtils.java#L2262-L2350Find the Levenshtein distance between two Strings.
This is the number of changes needed to change one String into another, where each change is a single character modification (deletion, insertion or substitution).
The implementation uses a single-dimensional array of length s.length() + 1. See http://blog.softwx.net/2014/12/optimizing-levenshtein-algorithm-in-c.html for details.
StringUtils.getLevenshteinDistance(null, *) = IllegalArgumentException StringUtils.getLevenshteinDistance(*, null) = IllegalArgumentException StringUtils.getLevenshteinDistance("", "") = 0 StringUtils.getLevenshteinDistance("", "a") = 1 StringUtils.getLevenshteinDistance("aaapppp", "") = 7 StringUtils.getLevenshteinDistance("frog", "fog") = 1 StringUtils.getLevenshteinDistance("fly", "ant") = 3 StringUtils.getLevenshteinDistance("elephant", "hippo") = 7 StringUtils.getLevenshteinDistance("hippo", "elephant") = 7 StringUtils.getLevenshteinDistance("hippo", "zzzzzzzz") = 8 StringUtils.getLevenshteinDistance("hello", "hallo") = 1- Parameters:
s- the first String, must not be nullt- the second String, must not be null- Returns:
- result distance
- Throws:
IllegalArgumentException- if either String inputnull
-