1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | public class StringSearch { public static void main(String[] args) { String sentence = "Made in india"; String subStr1 = "in"; String subStr2 = "out"; String subStr3 = "india"; int i = sentence.indexOf(subStr1); if (i != -1) { System.out.println(subStr1 + " is present in given sentence "); } else { System.out.println(subStr1 + " is not present in given sentence "); } if (sentence.indexOf(subStr2) != -1) { System.out.println(subStr2 + " is present in given sentence "); } else { System.out.println(subStr2 + " is not present in given sentence "); } if (sentence.contains(subStr3)) { System.out.println(subStr3 + " is present in given sentence "); } else { System.out.println(subStr3 + " is not present in given sentence "); } } } |
indexOf(String str) return int value that is the index value of str if found in string other wise it returns -1.The index value start from 0.
contains(CharSequence str) returns boolean value if str is present in given string it will return true else false.