The String#indexOf
method will try to find the full text of what you indicate within the text string. Since your text string has the value "sql"
and you are looking for "lenguaje sql"
, you will not find it since there is no trace of "lenguaje "
.
What you are trying to do is a forced one and maybe you should not do it. On the other hand, it may be that whatever you want to do is search if any of the "words" within your text string can be embedded in the string. For this, you could use the following algorithm:
String cadenaDondeBuscar = "sql";
String loQueQuieroBuscar = "lenguaje sql";
String[] palabras = loQueQuieroBuscar.split("\s+");
for (String palabra : palabras) {
if (cadenaDondeBuscar.contains(palabra)) {
System.out.println("Encontrado");
//aquí tu lógica en caso que se haya encontrado...
}
}
For Mariano's comment, if you want to evaluate the string by removing not only the blank spaces but also any other character that is not a vowel or consonant, we use \W+
to separate the string:
String cadenaDondeBuscar = "sql";
String loQueQuieroBuscar = "lenguaje sql";
String[] palabras = loQueQuieroBuscar.split("\W+");
for (String palabra : palabras) {
if (cadenaDondeBuscar.contains(palabra)) {
System.out.println("Encontrado");
//aquí tu lógica en caso que se haya encontrado...
}
}