java 如何与字符串部分匹配?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43760107/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 07:42:09  来源:igfitidea点击:

How to partial match with a string?

java

提问by Crazymango

How can I type partial letters of a word to find this word?

如何输入一个单词的部分字母来找到这个单词?

For example: I have a string array

例如:我有一个字符串数组

 String[] s = {"Cartoon", "Cheese", "Truck", "Pizza"};

if I input partial letters, such as "ca","Che" or "piz"

如果我输入部分字母,例如“ca”、“Che”或“piz”

then I can find the whole words for the list.

然后我可以找到列表的整个单词。

Thanks

谢谢

回答by Danyal Sandeelo

stringValue.contains("string that you wanna search");

.containswill do the job, iterate over the loop and keep adding the words in ArrayList<String>for the matched ones.

.contains将完成这项工作,遍历循环并继续ArrayList<String>为匹配的单词添加单词。

This could be a good readon string in java.

这可以很好地阅读java 中的字符串。

.containsis like '%word%'in mysql. There are other functions like .startsWithand .endsWithin java. You may use whatever suits the best.

.contains就像'%word%'在mysql中一样。在java中还有其他功能,如.startsWith.endsWith。您可以使用最适合的方式。

回答by Imaginary Pumpkin

Try using String#startsWith

尝试使用String#startsWith

List<String> list = new ArrayList<>();
list.add("Apples");
list.add("Apples1214");
list.add("NotApples");
list.stream().map(String::toLowerCase)
             .filter(x->x.startsWith("app"))
             .forEach(System.out::println);

If you just want the Stringto be contained, then use String#contains

如果您只想String包含 ,则使用String#contains

回答by Rudy B

Assuming you meant

假设你的意思是

String[] s = {"Cartoon", "Cheese", "Truck", "Pizza"};

The easiest option would be to iterate through your string array and do a .contains on each individual String.

最简单的选择是遍历您的字符串数组并对每个单独的字符串执行 .contains 。

for(int i = 0; i < s.length; i++){
   if(s[i].contains("car")){
      //do something like add to another list.
   }
 }

This ofcourse does not take caps into concideration. But this can easily be circumvented with .toLowerCare or .toUpperCase.

这当然不考虑上限。但这可以通过 .toLowerCare 或 .toUpperCase 轻松规避。

回答by Abhishek

Try This Logic For Basic

试试这个基本的逻辑

    Scanner sc=new Scanner(System.in);

    String[] s = {"Cartoon", "Cheese", "Truck", "Pizza",""};

    System.out.println("Enter Characters");

    String matchCharStr = sc.next();

    for (int i = 0; i < s.length; i++) {
        if (s[i].toLowerCase().contains(matchCharStr.toLowerCase())) {
            System.out.println(s[i]);
        }
    }

回答by barbakini

First cast all your strings upper or lower case and then use startsWith () method.

首先将所有字符串转换为大写或小写,然后使用 startsWith() 方法。

回答by patrick-hainge

You could use something like

你可以使用类似的东西

String userInput = (new Scanner(System.in)).next();
for (String string : s) {
    if (string.toLowerCase().contains(userInput.toLowerCase()) return string;
}

Note that this is only going to return the first string in your list that contains whatever the user gave you so it's fairly imprecise.

请注意,这只会返回列表中包含用户提供给您的任何内容的第一个字符串,因此它相当不精确。