我正在尝试创建一个比较两个字符串的布尔值,并获取指定的两个字符串中匹配字符的百分比。如果百分比大于或等于80,则返回true。到目前为止我的工作方式,但感觉有点便宜,而且我想知道是否有更好的方法来做到这一点。这就是我所拥有的:
public static boolean isMatch(String word, String comparison) {
int matches = 0;
char[] word_characters = word.toLowerCase().toCharArray();
char[] comparison_characters = comparison.toLowerCase().toCharArray();
int minLength = Math.min(word_characters.length, comparison_characters.length);
int highest = (word_characters.length > comparison_characters.length ? word_characters.length : comparison_characters.length);
for(int i = 0; i < minLength; i++) {
if (word_characters[i] == comparison_characters[i]) {
matches++;
}
}
int percent = (int) (matches * 100.0f) / highest;
return percent >= 80;
}