java-string-examplesStrings

How to check if a String is a substring of another String

Posted On
Posted By admin

In this blog post, I will be explaining how you can check if a String is a substring of another String. Consider the following code snippet:


package learnjava.strings;

public class CheckSubstringDemo {

public static void main(String[] args) {
String strToCheck = "Hello World";
String valueToCheck = "Hello";

//Method 1 - Use Contains
if(strToCheck.contains(valueToCheck))
System.out.println(valueToCheck+" is present in "+strToCheck);
else
System.out.println(valueToCheck+" is NOT present in "+strToCheck);

//Method 2 - Use startsWith and indexOf
if(strToCheck.startsWith(valueToCheck) || strToCheck.indexOf(valueToCheck) > 0)
System.out.println(valueToCheck+" is present in "+strToCheck);
else
System.out.println(valueToCheck+" is NOT present in "+strToCheck);


}

}


 

This code snippet demonstrates two ways to check if a String is a substring of another String.

Method 1:

This method uses the String.contains method. This returns true if the String on which it is invoked contains the String that is specified. It accepts a CharSequence as input. CharSequence is an interface. The String class implements this interface and so you can pass in a String value to this method.

 

Method 2:

This method uses the String.indexOf and String.startsWith method. The String.indexOf  method returns the position in the String on which it is invoked of the first occurrence of the String passed in. So if the String passed in is a substring, this method returns a non zero value. However, there is a small catch to this. If the String passed is is at the start, then this method will return a 0 since it just returns the position at which the String is present. So if we just use the  String.indexOf method, our code will print a wrong output when the specified String is at the starting position. So to avoid this situation,  the String.startsWith method is also used. This method returns a true if the String on which it is invoked starts with the specified String. So our code uses the String.indexOf and String.startsWith to check for both conditions.

When the above code snippet is run, it will produce the following output:

Hello is present in Hello World
Hello is present in Hello World

Note that both these methods are case sensitive. So for ex. if the valueToCheck is changed to “HELLO”, the following output will be printed:

HELLO is NOT present in Hello World
HELLO is NOT present in Hello World

If you want to perform a case insensitive search, you need to convert both the input String as well as the String to check to either lowercase or uppercase and then perform the check like this:


if(strToCheck.toLowerCase().contains(valueToCheck.toLowerCase()))
System.out.println(valueToCheck+" is present in "+strToCheck);
else
System.out.println(valueToCheck+" is NOT present in "+strToCheck);

 

This will print the following output:

HELLO is present in Hello World
If you like this post, please do let me know via the comments box below.  You can also connect with me via my Facebook Page or subscribe to my Youtube channel!

Related Post

leave a Comment