简体   繁体   English

改善阵列中的搜索

[英]Improve search in array

I'm a newbie to Android and Java so please be nice :) 我是Android和Java的新手,所以请保持友好:)

I have an EditText in my application which is used to search for a particular string in a String[] 我的应用程序中有一个EditText ,用于在String[]搜索特定的字符串。

My code works well, but not as I want: 我的代码运行良好,但不是我想要的:

ArrayList<String> allProd = new ArrayList<String>;
ArrayList<String> allProd_sort = new ArrayList<String>;

allProd = [the table is brown, the cat is red, the dog is white];

String[] allProdString = allProd.toArray(new String[allProd.size()]);

...

 //inputSearch is the EditText
 inputSearch.addTextChangeListener (new TextWatcher() {

   ...

   @Override
   public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) { 

     int textLength = inputSearch.getText().length();
     String text = inputSearch.getText().toString();

     for (int y = 0; y< allProdString.length; y++) {

        //in my case i want that the search start when there are min 3 characters in inputSearch
        if(textLength <= allProdString[y].length() && textLength >=3) {

           if (Pattern.compile(Pattern.quote(text), Pattern.CASE_INSENSITIVE)
                                .matcher(allProdString[y]).find()) {

               allProd_sort.add(allProdString[y]);

           }

        }
     }

   }

 });

This code produces these results: 此代码产生以下结果:

if I search for "table is" => allProd_sort will be [the table is brown] 如果我搜索“表是” => allProd_sort将是[the table is brown]

but if I search for "table brown" => allProd_sort will be empty but I want [the table is brown] 但是如果我搜索“ table brown” => allProd_sort将为空,但我想要[the table is brown]

how can I improve this? 我该如何改善呢?

thanks everybody 谢谢大家

You need to change the pattern. 您需要更改模式。 At the moment the string needs to contain exactly what you entered. 目前,该字符串需要准确包含您输入的内容。 If instead of the space you put "table.*brown" it would match. 如果用空格代替“ table。* brown”,它将匹配。

You can either have the user enter the regex, or you can do a simplified thing yourself by replacing all whitespace in the query string with ".*" before using it to match on. 您既可以让用户输入正则表达式,也可以自己做一个简化的事情,在使用匹配条件之前,将查询字符串中的所有空格替换为“。*”。

You can read all about regular expressions here: http://docs.oracle.com/javase/tutorial/essential/regex/ 您可以在此处阅读有关正则表达式的所有信息: http : //docs.oracle.com/javase/tutorial/essential/regex/

Ok - first optimization: Only enter the loop, if your initial requirement (Searchtext >=3) is true: 好的-第一个优化:如果您的初始要求(Searchtext> = 3)为true,则仅进入循环:

@Override
public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {

  int textLength = inputSearch.getText().length();

  if (textLength < 3) return;

  String[] searchPattern = inputSearch.getText().toString().toLowerCase().split("\\s+");

  for (int y = 0; y< allProdString.length; y++) {

    if(textLength <= allProdString[y].length()) {
       if (matchSearch(allProdString[y].toLowerCase(), searchPattern)) {

           allProd_sort.add(allProdString[y]);

       }

    }
 }

If you only want to match lines, where all words are contained in the same sequence you can simply create a regular Expression like Tim B said. 如果您只想匹配所有单词都包含在相同序列中的行,则可以像Tim B所说的那样简单地创建一个正则表达式。

But if you also want to match strings which contain the words anywhere search search "brown table" -> [the table is brown] then you need a little loop: 但是,如果您还想匹配包含单词的字符串,则在任何地方搜索search“ brown table”-> [the table is brown],那么您需要一个小循环:

public boolean matchSearch(String s, String[] searches) {
    for (String search : searches) {
        if (!s.contains(search) return false; // If the word is not in the string FALSE
    }
    return true; // If all words were found in the string, it is a match!
}

To make this a bit clearer -> brown.*table will only match Strings where table comes after brown... I don't think you can easily create an efficient RegEx to check if each word is at least one time anywhere in the string... 为了更清楚一点-> brown。* table只匹配table出现在brown之后的字符串...我认为您不能轻松创建有效的RegEx来检查每个单词在字符串中是否至少有一次...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM