fromMars

Search for substring

Apr 24th, 2017
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.73 KB | None | 0 0
  1. //The program below searches through a string and checks for a substring
  2. public class SearchlastString {
  3.    public static void main(String[] args) {
  4.       String strOrig = "Hello world ,Hello Reader";
  5.       int lastIndex = strOrig.lastIndexOf("Hello");
  6.      
  7.       if(lastIndex == - 1){
  8.          System.out.println("Hello not found");
  9.       } else {
  10.          System.out.println("Last occurrence of Hello is at index "+ lastIndex);
  11.       }
  12.    }
  13. }
  14.  
  15. //Alternatively you could do the same thing with the help of strOrig.lastIndexOf(Stringname)
  16.  
  17. public class HelloWorld{
  18.    public static void main(String []args) {
  19.       String t1 = "Tutorialspoint";
  20.       int index = t1.lastIndexOf("p");
  21.       System.out.println(index);
  22.    }
  23. }
Add Comment
Please, Sign In to add comment