Many times we need to check if a string has a sequence of characters and take some action based on the result.
Example, if you have a list of student records and you want to fetch a student record based on student last name.
The solution is to iterate over student records and for each record check if the name of student contains the desired last name.
There are a couple of methods in which this can be done in java.

Method 1: Using contains method
java.lang.String class has a contains method which takes another string as argument and checks for the presence of string inside another string.
It returns true if the string on which it is called has the string which is supplied to contains method as argument, false otherwise.
Example, "java".contains("String contains") will return false since “java” does not contain “String contains” and "javascript".contains("java") will return true since “javascript” contains “java”.
Remember that contains does a case sensitive comparison.

contains example
Below is an example that uses contains method to test for the presence of a string inside another string.

public class OverloadingDemo {

   public static void main(String[] args) {
     String lastName = "Snow";
     String name = "John Snow";
     // check if last name is present in name
     if (name.contains(lastName)) {
       System.out.println(lastName + " is present in " + name);
     } else {
       System.out.println(lastName + " is not present in " + name);

Above code prints below output

Snow is present in John Snow

Method 2: Using indexOf
java.lang.String class has an indexOf method which takes a string as argument and returns an integer.
This integer is the index of first occurrence of the supplied string argument inside the string on which indexOf is called.
If the supplied string argument is not present in the string on which this method is called, -1 is returned. Thus, in order to test if a string is present inside a string, indexOf with -1 can be used.
Note that string indexing starts at 0 meaning that the first character is at index 0, second at index 1 and so on.
"javascript".indexOf("java") will return 0; "burger".indexOf("urge") returns 1.
indexOf example
Example program demonstrating the use of indexOf method to test if a string is present inside another string.

public class OverloadingDemo {

   public static void main(String[] args) {
     String pizza= "Pizza";
     String burger = "Burger";
     // check if last name is present in name
     if (pizza.indexOf(burger) > -1) {
       System.out.println(burger + " is present in " + pizza);
     } else {
       System.out.println(burger + " is not present in " + pizza);

Remember that indexOf does a case sensitive comparison.
Thus, "hamburger".indexOf("burger") will return 3 while "hamburger".indexOf("Burger") will return -1.
Above program will print the following output.

Burger is not present in Pizza

Hit the clap if the article was useful!!!


Share your thoughts !!

Close Menu

Never Miss an article !

Get the new post delivered straight into your inbox, enter your email and hit the button

You have successfully subscribed to the newsletter

There was an error while trying to send your request. Please try again.

codippa will use the information you provide on this form to be in touch with you and to provide updates and marketing.