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);
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"));
}
}
You can run your code online here
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"));
}
}
You can run your code online here
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(""));
}
}
You can run your code online here
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));
}
}
You can run your code online here
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));
}
}
You can run your code online here
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"));
}
}
You can run your code online here
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");
}
}
}
You can run your code online here
Top comments (2)
Why did you add the "javascript" tag?
For your comment buddy!