Java is the most widely used object-oriented, multiple-platform, and reliable language which can be used as a forum in itself. Like other programming languages, Java also contains multiple functions and methods for performing different operations.
In this guide, we will briefly discuss the string “contains()” method in Java with the aid of multiple examples.
String contains() Method
The string “contains()” method is used to check whether the specified string or particular sequence of characters is present in the input string or not. It returns an answer in the boolean form which means that the output will be “true” or “false” only. If the input string contains a particular sequence of characters then the answer will be true otherwise the answer will be false.
Syntax
The general syntax of the string contains() method is provided below:
public boolean contains(CharSequence sequence)
Parameters
In the above-stated syntax:
- “sequence” is an argument and will be replaced with the particular sequence of characters that need to be searched.
Return type
- “boolean” is the return type of the “contains()” method.
Note: It will return the “NullPointerException” if the user passes a “null” as an argument.
Now, move ahead and check out the practical demonstration of the “contains()” method in Java via examples.
How to Check the Sequence of Characters in Java Using the “contains()” Method?
Let’s check out the below-given examples to demonstrate the working of the Java string contains() method.
Example 1: Check the Sequence of Characters is Exist or Not Using the “contains()” Method
A C# code example is given below that will show how to check if an input string has a word or a particular sequence of characters in it or not.
public class String_contains{
public static void main(String[] args) {
String str = "This is Java String contains() Method example";
System.out.println(str.contains("Linux"));
System.out.println(str.contains("example"));
}
}
In this example:
- First, we declare the public class named “String_constratins”.
- Then, inside the “main()” method, we create a string type “str” variable and initialize it with the string.
- After that, call the “contains()” method with the desired sequence of characters as an argument inside the “System.out.println()” method to check and display the output.
- Here, we have passed the “Linux”, and “example” as an argument respectively to check whether they exist in the input string or not:
Output
According to the below-given output:
- The output for “Linux” is “false” because this sequence of characters is not present in the given string.
- The output for “example” is “true” because it exists the same as in the provided input string:

Example 2: Case-sensitive Scenario to Check Whether a Sequence of Characters is Exist or Not Using the “contains()” Method
The “contains()” method searches case-sensitive sequences of characters. If the provided argument is not case-sensitive, then it will return false. Otherwise, it will be true. Let’s take another example to show results in a more meaningful way:
public class String_contains{
public static void main(String[] args) {
String str = "This is Java String contains() Method example";
System.out.println("The String contains sequence 'Method': " + str.contains("Method"));
System.out.println("The String contains sequence 'Example': " +str.contains("Example"));
System.out.println("The String contains sequence 'ing': " +str.contains("ing"));
}
}
Here, we take the previously discussed example and add the additional text inside the “System.out.println()” method to make the output more precise and understandable why “true” and “false” are there.
Output
The below result will show the impact of changing the case on the output.

Example 3: Check the Sequence of Characters is Exist or Not Using the “contains()” Method With Condition Statement (if else)
You can also use the “contains()” method inside the “if else” condition statement to get the search-based results. In simple words, users can get the output without true and false by using if else statements. For this purpose, check out the provided codes.
For true case
First, we will get the output for the “true” result, if the specified sequence of character arguments is present in the string then it will execute the “if” statement:
public class String_contains{
public static void main(String[] args) {
String str = "This is Java String contains() Method example";
if (str.contains("example")) {
System.out.println("The Keyword example is found in given string");
} else {
System.out.println("The Keyword example is not found in the string");
}
}
}
Output
The output will print the statement present in “if” block because “if” condition is met.

For false case
Now, check for the “false” condition:
public class String_contains{
public static void main(String[] args) {
String str = "This is Java String contains() Method example";
if (str.contains("Example")) {
System.out.println("The Keyword example is found in given string");
} else {
System.out.println("The Keyword example is not found in the string");
}
}
}
Output
The statement inside the else block will be printed here because the if condition is not fulfilled here.

As we discussed above, if a user passes the null as an argument to the “contains()” method. Then, it will throw the “NullPointerException”. For better understanding, check out the below-given line of code:
public class String_contains{
public static void main(String[] args) {
String str = "This is Java String contains() Method example";
System.out.println(str.contains(null));
}
}
Output

Conclusion
In Java, the “contains()” method is used for checking whether the provided sub-string or particular sequence of characters exists in the input string or not. It returns output in the form of boolean (true/false). In this guide, we have described the “contains()” method with the aid of multiple examples.