211 lines
5.3 KiB
Java
211 lines
5.3 KiB
Java
package com.base.helper;
|
||
|
||
import java.util.ArrayList;
|
||
import java.util.Arrays;
|
||
import java.util.HashMap;
|
||
import java.util.Iterator;
|
||
import java.util.List;
|
||
import java.util.Map;
|
||
|
||
import com.amarsoft.are.lang.StringX;
|
||
|
||
/**
|
||
* 字串通用工具处理类
|
||
* @author yangsong
|
||
* @date 2014/06/18
|
||
*
|
||
*/
|
||
public class StringHelper {
|
||
|
||
/**
|
||
* 空串
|
||
* @param s 字串
|
||
* @param s1 字串为空时,返回的字串
|
||
* @return
|
||
*/
|
||
public static String nvl(String s,String s1){
|
||
if(StringX.isEmpty(s))return s1;
|
||
return s;
|
||
}
|
||
|
||
/**
|
||
* 连接字串
|
||
* @param strList
|
||
* @param delimiter
|
||
* @return
|
||
*/
|
||
public static String join(List<String> strList,String delimiter){
|
||
StringBuilder sb = new StringBuilder();
|
||
for(int i=0;i<strList.size();i++){
|
||
if(i!=0)sb.append(delimiter);
|
||
sb.append(strList.get(i));
|
||
}
|
||
return sb.toString();
|
||
}
|
||
|
||
/**
|
||
* 比较两个字串,返回他们不同的字符个数
|
||
* @param str
|
||
* @param target
|
||
* @return
|
||
*/
|
||
public static int compare(String str, String target) {
|
||
int d[][]; // 矩阵
|
||
int n = str.length();
|
||
int m = target.length();
|
||
int i; // 遍历str的
|
||
int j; // 遍历target的
|
||
char ch1; // str的
|
||
char ch2; // target的
|
||
int temp; // 记录相同字符,在某个矩阵位置值的增量,不是0就是1
|
||
|
||
if (n == 0) return m;
|
||
if (m == 0) return n;
|
||
d = new int[n + 1][m + 1];
|
||
|
||
for (i = 0; i <= n; i++) { // 初始化第一列
|
||
d[i][0] = i;
|
||
}
|
||
|
||
for (j = 0; j <= m; j++) { // 初始化第一行
|
||
d[0][j] = j;
|
||
}
|
||
|
||
for (i = 1; i <= n; i++) { // 遍历str
|
||
ch1 = str.charAt(i - 1);
|
||
// 去匹配target
|
||
for (j = 1; j <= m; j++) {
|
||
ch2 = target.charAt(j - 1);
|
||
if (ch1 == ch2) {
|
||
temp = 0;
|
||
} else {
|
||
temp = 1;
|
||
}
|
||
|
||
// 左边+1,上边+1, 左上角+temp取最小
|
||
d[i][j] = min(d[i - 1][j] + 1, d[i][j - 1] + 1, d[i - 1][j - 1] + temp);
|
||
}
|
||
}
|
||
|
||
return d[n][m];
|
||
}
|
||
|
||
private static int min(int one, int two, int three) {
|
||
return (one = one < two ? one : two) < three ? one : three;
|
||
}
|
||
|
||
/**
|
||
* 计算两个字串的相似度
|
||
* @param str
|
||
* @param target
|
||
* @return
|
||
*/
|
||
public static double getSimilarityRatio(String str, String target) {
|
||
return 1 - (double) compare(str, target) / Math.max(str.length(), target.length());
|
||
|
||
}
|
||
|
||
/**
|
||
* 相似度匹配排序,比较和源字串的相似度,相似度最高的放到最前
|
||
* @param srcStr 源字串
|
||
* @param minRatio 最小匹配度,低于此值的,将不会出现到返回值中
|
||
* @param strList 目标字串列表
|
||
* @return
|
||
*/
|
||
public static List<String> sortSimilarity(String srcStr,double minRatio,String ...strList){
|
||
return sortSimilarity(srcStr,minRatio,Arrays.asList(strList));
|
||
}
|
||
/**
|
||
* 相似度匹配排序,比较和源字串的相似度,相似度最高的放到最前
|
||
* @param srcStr 源字串
|
||
* @param minRatio 最小匹配度,低于此值的,将不会出现到返回值中
|
||
* @param strList 目标字串列表
|
||
* @return
|
||
*/
|
||
public static List<String> sortSimilarity(String srcStr,double minRatio,List<String> strList){
|
||
List<String> simList = new ArrayList<String>();
|
||
//筛选
|
||
Map<String,String> simMap = new HashMap<String,String>();
|
||
for(int i=0;i<strList.size();i++){
|
||
String str = strList.get(i);
|
||
double simRatio = getSimilarityRatio(srcStr,str);
|
||
if(simRatio<minRatio)continue;
|
||
String key = ""+simRatio;
|
||
if(simMap.containsKey(key))key = key+"_1";
|
||
simMap.put(key, str);
|
||
}
|
||
//排序
|
||
while(simMap.size()>0){
|
||
String s = getMaxKeyValue(simMap);
|
||
if(s!=null)simList.add(s);
|
||
}
|
||
|
||
return simList;
|
||
}
|
||
|
||
private static String getMaxKeyValue(Map<String,String> simMap){
|
||
if(simMap.size()==0)return null;
|
||
String max = "";
|
||
Iterator<String> iterator = simMap.keySet().iterator();
|
||
while(iterator.hasNext()){
|
||
String i = iterator.next();
|
||
if(i.compareTo(max)>0){
|
||
max = i;
|
||
}
|
||
}
|
||
String r = simMap.get(max);
|
||
simMap.remove(max);
|
||
return r;
|
||
}
|
||
|
||
/**
|
||
* 把字符串数组转化成SQL语句中的IN条件。
|
||
* 例如:list={1:"a",2:"b",3:"c"},转化成'a','b','c'
|
||
* @param list
|
||
* @return
|
||
*/
|
||
public static String getSqlFormatOfIn(List<String> list){
|
||
String returnStr = "";
|
||
if (list == null)
|
||
return "''";
|
||
|
||
for(String item : list)
|
||
returnStr += "'" + item + "',";
|
||
|
||
if (returnStr.length() < 1)
|
||
return "''";
|
||
|
||
return returnStr.substring(0, returnStr.length() - 1);
|
||
}
|
||
|
||
/**
|
||
* 把带有分隔符的字符串转化成SQL语句中的IN条件。
|
||
* 例如:source="a,b,c",format=",",转化成'a','b','c'
|
||
* @param source
|
||
* @param format
|
||
* @return
|
||
*/
|
||
public static String getSqlFormatOfIn(String source, String format){
|
||
if(StringX.isEmpty(format))
|
||
format = ",";
|
||
return getSqlFormatOfIn(source.split(format));
|
||
}
|
||
|
||
/**
|
||
* 把字符串数组转化成SQL语句中的IN条件。
|
||
* 例如:strs={"a","b","c"},转化成'a','b','c'
|
||
* @param strs
|
||
* @return
|
||
*/
|
||
public static String getSqlFormatOfIn(String[] strs){
|
||
String returnStr = "";
|
||
for(String item : strs)
|
||
returnStr += "'" + item + "',";
|
||
|
||
if (returnStr.length() < 1)
|
||
return "''";
|
||
|
||
return returnStr.substring(0, returnStr.length() - 1);
|
||
}
|
||
}
|