Java中对比两个字符串的相似度的方法, 以下整理了两个方式比对方法,同样的字符串不同的计算方式得到的结果也是不同的:
package test;
/**
* 对比俩个字符串的相似度
* @author sanshi
*/
public class StrUtil {
/**
* 获取最长子串 (参数顺序与字符串长短无关)
* @param strA
* @param strB
* @return
*/
public static String longestCommonSubstringNoOrder(String strA, String strB) {
if(strA.length() >= strB.length()){
return longestCommonSubstring(strA, strB);
}else{
return longestCommonSubstring(strB, strA);
}
}
/**
* 获取最长子串 (长串在前,短串在后)
* @param strLong
* @param strShort
* @return
* <p>summary</p>:较长的字符串放到前面有助于提交效率
*/
private static String longestCommonSubstring(String strLong, String strShort) {
char[] chars_strA = strLong.toCharArray();
char[] chars_strB = strShort.toCharArray();
int m = chars_strA.len