获取格式化查询字符串
2021/9/30 6:12:40
本文主要是介绍获取格式化查询字符串,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
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); } }
这篇关于获取格式化查询字符串的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-26JAVA语音识别项目资料的收集与应用
- 2024-11-26Java语音识别项目资料:入门级教程与实战指南
- 2024-11-26SpringAI:Java 开发的智能新利器
- 2024-11-26Java云原生资料:新手入门教程与实战指南
- 2024-11-26JAVA云原生资料入门教程
- 2024-11-26Mybatis官方生成器资料详解与应用教程
- 2024-11-26Mybatis一级缓存资料详解与实战教程
- 2024-11-26Mybatis一级缓存资料详解:新手快速入门
- 2024-11-26SpringBoot3+JDK17搭建后端资料详尽教程
- 2024-11-26Springboot单体架构搭建资料:新手入门教程