Question:
How do you search for a word inside a string?
Answer:
Use the String method indexOf().
Code:
public class FindWord { public static void main(String[] args) { String str1 = "The cat in the hat."; System.out.println(str1); // find this word String str2 = "cat"; int index = str1.indexOf(str2); if(index >= 0) { System.out.println("The word " + str2 + " found"); System.out.println("index " + index); } else { System.out.println(str2 + " not found"); } } }
Output:
$ java FindWord The cat in the hat. The word cat found index 4