当前位置:   article > 正文

JAVA 实现字符串(String)的模糊查找_java 模糊搜索

java 模糊搜索

花了点时间,实现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"类似的字符串,否则检索不到;

  • 1
  • 2
  • 3
  • 4
  • 5

直接上代码(说明:下面代码基于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
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98

验证:

/*****测试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!!

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/499116
推荐阅读
相关标签
  

闽ICP备14008679号