DEV Community

Cover image for Java contains() method
Ritvik Dubey
Ritvik Dubey

Posted on

Java contains() method

Hello all๐Ÿ˜€ I hope you are doing well. This is going to be a very short and very useful article. In this article I'll be writing about contains() method in Java. This method is very useful while you are doing competitive programming.

Let's begin...

contains()

This is a method of Java String class. This method returns a boolean value. This method checks if the specified substring is present in the given string. If substring is their then it returns true else it returns false. This method searches the sequence of characters in the given string. Remember this method is case-sensitive.

Syntax :-

stringVariableName.contains(substring);
Enter fullscreen mode Exit fullscreen mode

Example :-

public class Demo {
    public static void main(String[] args){
        String str = "Hey, I'm the main string here!";
        System.out.println(str.contains("main string"));
    }
}
Enter fullscreen mode Exit fullscreen mode

You can run your code online here

java-contains().png

Remember it searches for the sequence of characters in the given string. This will be more clear from this example :-

public class Demo {
    public static void main(String[] args){
        String str = "Hey, I'm the main string here!";
        System.out.println(str.contains("Hey string"));
    }
}
Enter fullscreen mode Exit fullscreen mode

You can run your code online here
java-contains()-false.png

This example will return true, we will pass an empty string in contains method but still it will return true because empty is a subset of any string.

public class Demo {
    public static void main(String[] args){
        String str = "Hey, I'm the main string here!";
        System.out.println(str.contains(""));
    }
}
Enter fullscreen mode Exit fullscreen mode

You can run your code online here

java-contains()-empty-string.png

If we pass null in the contains() it will throw a NullPointerException

public class Demo {
    public static void main(String[] args){
        String str = "Hey, I'm the main string here!";
        System.out.println(str.contains(null));
    }
}
Enter fullscreen mode Exit fullscreen mode

You can run your code online here

java-contains()-null.png

There is one more way how you can use contains(), by passing string variable instead of passing a string itself.

public class Demo {
    public static void main(String[] args){
        String str = "Hey, I'm the main string here!";
        String subStr = "I'm the main";
        System.out.println(str.contains(subStr));
    }
}
Enter fullscreen mode Exit fullscreen mode

You can run your code online here

java-contains()-var.png

We know that contains() method is case-sensitive, however there is a way you can check for the substring in the given string, using toUpperCase() or using toLowerCase() methods since both these methods and contains() method are the methods of same Java String class

public class Demo {
    public static void main(String[] args){
        String str = "Hey, I'm the main STRING here!";
        System.out.println(str.toLowerCase().contains("the main string"));
    }
}
Enter fullscreen mode Exit fullscreen mode

You can run your code online here

java-contains()-toLowerCase().png

Since contains() method returns boolean value we can use it with if else conditional statements

import java.util.Scanner;

public class Demo {
    public static void main(String[] args){
        String str = "Team Leader :- Ritvik Dubey";
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter team leader name");
        String subStr = sc.nextLine();
        sc.close();
        if(str.contains("Ritvik Dubey")) {
            System.out.println("Yes, team leader confirmed");
        }
        else {
            System.out.println("Team leader not confirmed");
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

You can run your code online here

java-contains()-if-else.png

Okay so that's enough for now follow my this journey to learn more about Java.

Thank you for reading.

Please share your thoughts about it and correct me if I'm wrong.

I hope you liked it and found it helpful.

Cover:- Rajat Gour

Connect with me on Twitter or LinkedIn

My personal blog blog.ritvikdubey.com

Top comments (2)

Collapse
 
thorstenhirsch profile image
Thorsten Hirsch

Why did you add the "javascript" tag?

Collapse
 
ritvikdubey27 profile image
Ritvik Dubey

For your comment buddy!