Parameter of contains() method
sequence: It specifies the sequence of characters to be searched.
Exception of contains() method
NullPointerException: If the sequence is null
Returns Value of contains() method
true if the sequence of char values exist; otherwise, false.
Implementation of contains() method
public boolean contains(CharSequence sequence)
{
return indexOf(sequence.toString()) > -1;
}
In this case, CharSequence is converted to a String and then indexOf is called. If indexOf finds the String, it returns O or a higher number, otherwise -1. In other words, if a sequence of char values exists, the contains() method returns true, otherwise it returns false.
Example of the java.string.contains() method
Let us look over a few examples to understand the concepts better.
Example 1: To check whether the charSequence is present or not.
class Ninja1 {
public static void main(String args[]) {
String var1 = "My company's name is CodingNinjas";
// Checking if "CodingNinjas" is present in var1
System.out.println(var1.contains("CodingNinjas")); // This will print true
// Declare and initialize a new string for another check
String s1 = "CN";
System.out.println(var1.contains(s1)); // This will print false since "CN" is not a part of var1
}
}

You can also try this code with Online Java Compiler
Run Code
Output:
true
false
Example 2: Case-sensitive method to check whether given CharSequence is present or not.
class Ninja2 {
public static void main(String args[]) {
String var2 = "CodingNinjas";
// Checking if 'var2' contains "Ninjas"
System.out.println(var2.contains("Ninjas")); // This will print true
// Checking if 'var2' contains "NINJAS" (case-sensitive check)
System.out.println(var2.contains("NINJAS")); // This will print false
}
}

You can also try this code with Online Java Compiler
Run Code
Output:
true
false
Limitations of Java string contains() method
-
Case Sensitivity: The method is case-sensitive, making it unable to perform case-insensitive searches directly.
-
No Regular Expression Support: contains() only checks for literal substrings and does not support regex.
-
Performance with Large Strings: It can be inefficient with large strings, especially when used frequently, due to its linear time complexity.
-
Null Handling: Passing a null argument to contains() will result in a NullPointerException, requiring manual null checks.
-
No Index Information: It only returns a boolean, without indicating the position of the substring in the string.
-
Immutable Strings: Since Java strings are immutable, contains() cannot modify the string; it only checks for the substring's presence.
Frequently Asked Questions
How do I check if a string contains a specific sequence of characters?
The String.contains() method is used to search the sequence of characters in the given string. If the sequence of char values is found in this string, it returns true, otherwise returns false.
How do you compare strings in Java?
The equals() method is used to compare two strings in java.
How do I find a word in a string in Java?
To find a word in the string, we are using indexOf() and contains() methods of String class. The indexOf() method is used to find an index of the specified substring in the present string.
What does string () do in Java?
In Java, String() is a constructor that creates a new String object. It can initialize a string from various sources, such as character arrays, byte arrays, or another string. While strings are usually created using literals, the String() constructor allows for more flexible and explicit string creation.
Conclusion
The Java String contains() Method is a straightforward and effective tool for checking the presence of a substring within a string. While it is case-sensitive and lacks regex support, it serves well for simple substring checks. However, for complex searches or large datasets, consider its limitations and explore alternative methods to optimize performance and functionality in your Java applications.
Also read:
We hope that this blog has helped you enhance your knowledge, and if you wish to learn more, here are some courses provided by Coding Ninjas: Basics of C++ with DSA, Competitive Programming and MERN Stack Web Development.