本文主要是介绍获取格式化查询字符串,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
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);
}
}
这篇关于获取格式化查询字符串的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!