获取格式化查询字符串
作者:互联网
package com;
import java.util.*;
public class MethodTest {
public static void main(String[] args) {
System.out.println(getFormatStr("LW53_CQ_0207"));
}
/**
* 获取查询字符串的格式化查询列表
* @param queryStr
* @return
*/
static List<String> getFormatStr(String queryStr){
Set<String> strKeySet = new TreeSet(){{
add("_");
add(".");
add("-");
}};
Set<Character> charKeySet = new TreeSet(){{
add('_');
add('.');
add('-');
}};
List<String> resultList = new ArrayList();
List<Map<Integer,Character>> positionList = new ArrayList();
if (!containKey(queryStr,strKeySet)){
resultList.add(queryStr);
return resultList;
}
char[] queryStrChar = queryStr.toCharArray();
for (int i = 0; i < queryStrChar.length; i++) {
if (isKey(queryStrChar[i],charKeySet)) {
HashMap map = new HashMap();
map.put(i,queryStrChar[i]);
positionList.add(map);
}
}
Collections.reverse(positionList);
StringBuilder sb = new StringBuilder(queryStr);
for (Map<Integer, Character> positionMap : positionList) {
resultList.add(sb.toString());
sb = new StringBuilder(addQueryStr(resultList,sb,positionMap,charKeySet));
}
resultList.add(sb.toString());
return resultList;
}
/**
* 判断字符串是否包含关键字
* @param str
* @param strKeySet
* @return
*/
static boolean containKey(String str, Set<String> strKeySet){
for (String s : strKeySet) {
if (str.contains(s)) {
return true;
}
}
return false;
}
/**
* 判断当前字符是否是key
* @param str
* @param keySet
* @return
*/
static boolean isKey(char str,Set<Character> keySet){
if (keySet.contains(str))
return true;
return false;
}
/**
* 获取同级别长度格式化查询字符串列表
* @param list
* @param sb
* @param positionMap
* @param keySet
* @return
*/
static String addQueryStr(List<String> list,StringBuilder sb, Map<Integer,Character> positionMap, Set<Character> keySet){
int position = 0;
char key = '.';
for (Map.Entry<Integer, Character> integerCharacterEntry : positionMap.entrySet()) {
position = integerCharacterEntry.getKey();
key = integerCharacterEntry.getValue();
}
for (Character character : keySet) {
if (character == key)
continue;
list.add(sb.replace(position, position + 1, String.valueOf(character)).toString());
}
return sb.substring(0, position);
}
}
标签:return,resultList,param,查询,add,sb,字符串,格式化,new 来源: https://www.cnblogs.com/huangshiyun/p/15355055.html