花了点时间,实现JAVA 字符串(String)的模糊查找;
目的:实现用户输入关键字,查找含有该关键字的字符串,如:
输入: I love Java
返回: My name is GG, i love IT, java is good!! (该字段含有 "i" "love" "java" 的字段(不区分大小写),所以能检索到)
注意: 1.关键字输入检索时有顺序要求,如输入"java love i", 如果没有包含该顺序关键字的字符串,是检索不到的;2. 输入有重复关键字时,检索也会按重复关键字去检索,如输入"kaili kaili", 返回也必须有 "xxxkailixxx xxxkailixxx"类似的字符串,否则检索不到;
直接上代码(说明:下面代码基于idea-springboot,刚从C转Java,风格还保留C风格,大家看看就行, 如有问题,欢迎指正!):
package com.example.mystring;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;@SpringBootApplication
public class MyStringApplication {public static void main(String[] args) throws IOException {SpringApplication.run(MyStringApplication.class, args);String userStr;while (true){userStr = getData();func01(userStr);}}public static String getData() throws IOException {String userStr;System.out.print("Please input: ");BufferedReader str = new BufferedReader(new InputStreamReader(System.in));userStr = str.readLine();return userStr;}public static void func01(String str) {List<String> resList = new ArrayList<>();String sourceStr[] = {"rty ryry I enter a wer with wer fgffrom the java Console.","Hgow kaili I enter a strkailing with Spaertces wer the java wer.","Hoghw dyuyo I enter a strwerewing with dfg from the java dfg.","gh yu I 2323432 a kailikaili with dfg rty the java dfg.","gh jghy I werwer a strwerweing with dfg hj the java dfg.","gh do I werwe java kaili with Spaces from the java Console.","My name is GG, i love IT, java is good!!"};String[] strArray = str.toLowerCase().split("\\s+"); //str.toLowerCase() 先转小写, "\\s+" 是按空格分割。for (int i =0; i< strArray.length; i++) {System.out.println("proStr["+i+"]: " + strArray[i]);}/* 字符数组拼接StringJoiner joiner = new StringJoiner("-");for (String s : splited) {joiner.add(s);}System.out.println("pro: "+ joiner.toString());*/int count = 0;boolean isFind = false;for (int i = 0; i < sourceStr.length; i++) {boolean isOneFind = true;int indexTemp = 0;int len = 0;for (int j = 0; j < strArray.length; j++) {//这一步很关键!! sourceStr也要转小写,如果找到了,下一个关键字要从(当前位置+刚找到的关键字的长度)处开始查找int index = sourceStr[i].toLowerCase().indexOf(strArray[j], indexTemp+len);if (index < 0) {isOneFind = false;}else{len = strArray[j].length();indexTemp = index;}}if (isOneFind) {resList.add(sourceStr[i]);isFind = true;count++;}}if (isFind){//System.out.println("Is find, num = "+num +":\n" + Arrays.toString(resList.toArray()));System.out.println("Is find, count = "+count +":\n" + resList);}else{System.out.println("Not find!!");}}}
验证:
/*****测试1*****/
Please input: kaili kaili
proStr[0]: kaili
proStr[1]: kaili
Is find, count = 2:
[Hgow kaili I enter a strkailing with Spaertces wer the java wer., gh yu I 2323432 a kailikaili with dfg rty the java dfg.]/*****测试2*****/
Please input: kaili
proStr[0]: kaili
Is find, count = 3:
[Hgow kaili I enter a strkailing with Spaertces wer the java wer., gh yu I 2323432 a kailikaili with dfg rty the java dfg., gh do I werwe java kaili with Spaces from the java Console.]/*****测试3*****/
Please input: i love java
proStr[0]: i
proStr[1]: love
proStr[2]: java
Is find, count = 1:
[My name is GG, i love IT, java is good!!]/*****测试4*****/
Please input: java love i
proStr[0]: java
proStr[1]: lova
proStr[2]: i
Not find!!