How to search whether a string array contains in another string array. Using complete and incomplete words

Asked

Viewed 290 times

4

How to search in an array of strings using complete and incomplete orders, similar to what the phonebook does.

Example:

List array1 = ['Escola','Estadual','José','da','silva'];
Lista busca = ['José','Silva','Estad'];

I want Uca to contain in array1, doing research for complete and incomplete words.

I tried it this way:

void main() {
  List storeName = ['ESCOLA','ESTADUAL','SAO','JOAO'];
  List queryList = ['ESCOLA','SAO', 'JOAO'];

  List vazia = [];

  for(var i = 0; i< storeName.length;i++){
    for(var x = 0; x< queryList.length;x++){
      if(storeName[i].contains(queryList[x])){
       if(vazia.indexOf(storeName[i]) == -1){
         vazia.add(storeName[i]);
       }else{
         print('n tem');
       }
      }
    }
  }
  print(vazia);
}

But what I really want is if I do:

List array1 = ['Escola','Estadual','José','da','silva'];
Lista busca = ['José','Silva','Esta'];

busca in array1 = true

And if you search like this, be false:

List array1 = ['Escola','Estadual','José','da','silva'];
Lista busca = ['José','Silva','Estlp'];

busca in array1 = false

Another interesting example is:

.contains("Dart is nice", "nice Dart")); //true
.contains("Dart is nice", "nice Darte")); //false
.contains("Dart is nice", "is Dar")); //true
  • 1

    Edit your question and let us know what you have tried so far, so we can help you better.

  • 1

    It is important that you show the effort you have already made in your research and attempts. Putting an example code that is almost working is a way to help you solve your problem more punctually. When you put in the standards for the question to accept answers again I put one of the possible solutions to your case. If you have the example I use even your example and only adjust with what you need.

2 answers

1

One of the possible implementations:

void main() { 
  // Exemplo 1
  List array1 = ['Escola','Estadual','José','da','silva'];
  List buscas = ['José','Silva','Estad'];

  bool achouExemplo1 = pesquisar(array1, buscas);
  print("Exemplo 1: $achouExemplo1");


  // Exemplo 2
  String frase = "Dart is nice";
  String fraseBusca = "is dar";

  List array2 = frase.split(" ");  
  List buscas2 = fraseBusca.split(" ");

  bool achouExemplo2 = pesquisar(array2, buscas2, caseSensitive: false);
  print("Exemplo 2: $achouExemplo2");  
}


// função responsável por processar a busca nos arrays.
bool pesquisar(palavras, buscas, {caseSensitive = false}){  
  bool match = false;

  for(String busca in buscas){
    busca = caseSensitive ? busca : busca.toUpperCase();

    for(String palavra in palavras){
      String auxPalavra = caseSensitive ? palavra : palavra.toUpperCase();
      match = auxPalavra.contains(busca);

      if(match) break;      
    }

    if(!match) break;
  }

  return match;
}

The function pesquisar() has the option to do the search considering or not the case sensitive. The parameter is optional and if not informed it is assumed as false. I used an example of each in the proposed code. In example 2 you are using the parameter case sensitive on the call.

The code can be tested on Dartpad.

  • 1

    This worked perfectly, I am using until today. Sorry for the delay in feedback

1


As you will have two word lists, you should use one loop to compare your records, thus:

void main() {
  List<String> array1 = ['Escola','Estadual','José','da','silva'];
  List<String> busca = ['José','Silva','Esta'];

  bool exists = false;

  List<String> array1Lower = array1.map((item) => item.toLowerCase()).toList();

  for(String textoBusca in busca){     
    for(String value in array1Lower){     
      exists = value.contains(textoBusca.toLowerCase());
      if (exists)
        break;
    }

    if (!exists)
       break;
  }

  print(exists);
}

Explanation

How do you want it to work as a generic search caseInsensitive, it is necessary to convert all words of both lists to Lowercase, otherwise Dart will understand as separate words.

  • 1

    Thank you very much, it worked perfectly

Browser other questions tagged

You are not signed in. Login or sign up in order to post.