Java contains() method
Tags: javascript, java, beginners, codenewbie
Published: 2021-08-15 08:05:47

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 :-**

```Java
stringVariableName.contains(substring);
```  
**Example :-**

```Java
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"));
    }
}
``` 
***You can run your code online [here](https://www.tutorialspoint.com/compile_java_online.php)***

![java-contains().png](https://cdn.hashnode.com/res/hashnode/image/upload/v1629008730293/3hLJ6TfY3.png)

> Remember it searches for the sequence of characters in the given string. This will be more clear from this example :- 
```Java
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"));
    }
}
``` 
***You can run your code online [here](https://www.tutorialspoint.com/compile_java_online.php)***
![java-contains()-false.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1629009012380/Vl9DjO1iu.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.**

```Java
public class Demo {
    public static void main(String[] args){
	    String str = "Hey, I'm the main string here!";
	    System.out.println(str.contains(""));
    }
}
``` 
***You can run your code online [here](https://www.tutorialspoint.com/compile_java_online.php)***

![java-contains()-empty-string.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1629009272954/iKytZcaGw.png)

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

```Java
public class Demo {
    public static void main(String[] args){
		String str = "Hey, I'm the main string here!";
		System.out.println(str.contains(null));
    }
}
``` 
***You can run your code online [here](https://www.tutorialspoint.com/compile_java_online.php)***

![java-contains()-null.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1629010846959/m-weoi4s4.png)



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

```Java
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));
    }
}
``` 
***You can run your code online [here](https://www.tutorialspoint.com/compile_java_online.php)***

![java-contains()-var.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1629010426977/p4qkNjphQ.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**

```Java
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"));
    }
}
``` 
***You can run your code online [here](https://www.tutorialspoint.com/compile_java_online.php)***

![java-contains()-toLowerCase().png](https://cdn.hashnode.com/res/hashnode/image/upload/v1629011414573/Tj1FUhzEB.png)

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

```Java
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");
		}
    }
}
``` 
***You can run your code online [here](https://www.tutorialspoint.com/compile_java_online.php)***

![java-contains()-if-else.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1629013050717/BkSahiVzU.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](https://twitter.com/rajatgour20)***
#### *Connect with me on [Twitter](https://twitter.com/ritvikdubey27?s=09) or [LinkedIn](https://www.linkedin.com/in/ritvik-dubey-3bbb22169)*
#### *My personal blog [blog.ritvikdubey.com](https://blog.ritvikdubey.com/)*