Find part of a string within a vector

0

Well I have a vector that gets several lines of text. But I need to compare these lines and see if I find a word, I tried with equals () but it only returns true if it finds exactly the same string and contains () does not work with the vector. ex:

Word that I want to search for: tomato

vector text:

[0] = Lettuces are green.

[1] = The tomatoes are red.

[2] = Peppers are colorful.

I left the code like this. It returns me the line where the word is found which already serves my purposes.

public int buscaLinha(String[] vetor, String palavra)
{
   int i = 1;
   for(String p : vetor)
   {
       if(p.contains(palavra)) {
           return i;
       }
       i++;
   }

   return 0;
}

I would like to thank you for the attention you have given me.

    
asked by anonymous 12.03.2015 / 20:31

3 answers

3

You can try with the following code:

public boolean encontrarPalavra(String[] vetor, String palavra)
{
   for(String p : vetor)
   {
       if(p.contains(palavra))
         return true;
   }

   return false;
}
    
12.03.2015 / 20:39
0

PHP code example

public function buscarPalavra ($palavraBuscada, $array)
{
   if(in_array($palavraBuscada,$array)){
      echo "Palavra encontrada: $palavraBuscada";
   }else{
      echo "Palavra Não Localizada!";
   }
}
    
12.03.2015 / 20:44
0

I do not know if JAVA contains the Substring method, but if you have it maybe it's an option. I developed this in C # and it worked, as C # is relatively similar to JAVA, I put here this method to try to help as well. This method is similar to that placed by Murilo Fechio:

public static bool existe_no_vetor (string[] aux, string strpretendida)
{
    foreach (string str in aux)
        {
            if (str.Contains(strpretendida) == true)
                return true;
        }

    return false;
}

Good luck!

    
12.03.2015 / 21:04