how to check string contains substring or not using indexOf and contains method in java


java.lang.String Api provides methods like indexOf(string) and contains(CharSequence) to check string contains substring in java


Method 1>
Program to find out string contains substring in java using indexOf(string) method in java>



indexOf(string) - Returns the index of the first occurrence of the specified substring in string.
If string does not contains substring method simply returns -1.
public class StringContainSubstringUsingIndexOfMethod {
   public static void main(String[] args) {
          String string = "java made so easy";
         
          //Find index of 'made'
          //Returns the index of the first occurrence of the specified substring in string.
          if(string.indexOf("made") != -1){
                 System.out.println("index of 'made' is "+ string.indexOf("made"));
          }
          else{
                 System.out.println("string doesn't contains any substring 'made'");
          }
         
   }
}
/*OUTPUT
index of 'made' is 5
*/


Method 2>
Program to find out string contains substring in java using contains(CharSequence s) method in java>

contains(CharSequence s) - contains method Returns true if and only if string contains the specified sequence of char values. If string does not contains substring method simply returns false.
public class StringContainSubstringUsingContainsMethod {
   public static void main(String[] args) {
          String string = "java made so easy";
         
          //Find string contains substring 'made' or not
         
          //contains method Returns true if and only if string
          //contains the specified sequence of char values (i.e. 'made')
          if(string.contains("made")){
                 System.out.println("string contains substring 'made'");
          }
          else{
                 System.out.println("string doesn't contains any substring 'made'");
          }
         
   }
}
/*OUTPUT
string contains substring 'made'
*/




What about finding index of string from specified index>
Program to find out string contains substring in java from specified index using indexOf(string, int) method in java>


indexOf(string, int) - Returns the index of the first occurrence of the specified substring in string from specified index.
If string does not contains substring from specified index method simply returns -1.
public class StringContainSubstringUsingIndexOfMethod2 {
   public static void main(String[] args) {
          String string = "java made so easy made";
         
          //Find index of 'made', start searching from specified index.
          //Returns the index of the first occurrence of the specified substring in string from specified index.
          if(string.indexOf("made", 10) != -1){
                 System.out.println("index of 'made' is "+ string.indexOf("made", 10));
          }
          else{
                 System.out.println("string doesn't contains any substring 'made'");
          }
         
   }
}
/*OUTPUT
index of 'made' is 18
*/


In above program indexOf(string, int) will start searching ‘made’ from 10th index ‘s’ (i.e. in ‘so easy’ only)

No comments:

Post a Comment