Thursday, October 18, 2012

String contains() and indexOf() Example - Check String for SubString

Some time you want to check if one String contains another String or SubString or not e.g. "HelloWorld" contains "Hello" and "World" SubString. There are many ways to check SubString in String in Java e.g. contains(), indexOf() or matches() method of java.lang.String class. In our last article we have seen how to use matches method of String class with regular expression and In this article we will see example of contains() and indexOf() method to check any String or SubString in Java. contains and indexOf method are defined in java.lang.String class and used to check if String contains a particular SubString or not, only difference between contains() and indexOf() method is that, contains is added in Java 5 along with String matches() method while indexOf() is as old as equals(), hashCode and compareTo(). Another difference between contains() and indexOf() method is that contains() return boolean value e.g. true if String contains SubString and false if String doesn't contains SubString but indexOf() return int value, which is actually index of SubString i.e. starting location. indexOf() return -1 if source String does not contain SubString in Java.

String contains() and indexOf() Example in Java

How to use contains and indexOf method in Java StringLet's see one quick example of how to use contains() and indexOf() method of Java String class. Here we will used them to check if String contains a particular SubString or not. In this Java program we have used String "Hello World" and checking  for word "Hello" and "World" in this String.

/**
 *
 * Java program to check String for SubString. This program
 * demonstrate two ways to find if One String contains a particular word or not in Java.
 * contains() and indexOf() method of String class is used to check SubString
 * @author
 */

public class StringContainsExample {

 
    public static void main(String args[]) {

       String word = "Hello World";
     
       //indexOf return -1 if String does not contain specified word
       if(word.indexOf("World") != -1){
           System.err.printf("Yes '%s' contains word 'World' %n" , word);
       }else{
           System.err.printf("Sorry %s does not contains word 'World' %n " , word);
       }
     
       //contains method return boolean true if String contains specified word
       if(word.contains("Hello")){
           System.err.printf("Great '%s' contains word 'Hello' %n" , word);
       }else{
           System.err.printf("Sorry %s does not contains word 'Hello' %n" , word);
       }
     
    }
}

Output:
Yes 'Hello World' contains word 'World'
Great 'Hello World' contains word 'Hello'

If we re run this Java program again with different input e.g. checking for Helloo or Woorld SubString which is not in original String you will receive different output. By the way Another difference between contains() and indexOf() method is that contains() accept a CharSequence object as parameter which is a super interface for String, StringBuffer and StringBuilder in Java. You can pass either of those object to contains() method in Java.

These are two quick ways to check if one String contains other String, Character or Substring or not, which is very common programming question in Java interviews, along with how to compare two String in Java or how to split String in Java. There are lot of changes in Java API from Java 1.5 to Java 1.5 and it's still worth exploring. That's all on String contains() example and String indexOf() example in Java.

Other String articles in Java you may find useful:

1 comment:

Java67 Headline Animator