Advertisement
fromMars

Search a word

Apr 24th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.77 KB | None | 0 0
  1. /**This example shows how we can search a word within a String object using indexOf() method which returns a position index of a word within the string if found. Otherwise it returns -1.
  2. **/
  3. public class SearchStringEmp{
  4.    public static void main(String[] args) {
  5.       String strOrig = "Hello readers";
  6.       int intIndex = strOrig.indexOf("Hello");
  7.      
  8.       if(intIndex == - 1) {
  9.          System.out.println("Hello not found");
  10.       } else {
  11.          System.out.println("Found Hello at index " + intIndex);
  12.       }
  13.    }
  14. }
  15.  
  16. //This example shows how we can search a word within a String object
  17.  
  18. public class HelloWorld {
  19.    public static void main(String[] args) {
  20.       String text = "The cat is on the table";
  21.       System.out.print(text.contains("the"));
  22.    }
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement