Java String contains() Method
Get Job-ready: Java Course with 45+ Real-time Projects! - Learn Java
The contains() method in Java is a helpful string function that allows us to check if a particular String or character sequence occurs within another String. This method returns a boolean value indicating whether the sequence was found.
The essential signature of the contains() method is:
public boolean contains(CharSequence s)
It takes a single CharSequence parameter representing the sequence to search for. This can be another String or something like a StringBuilder. The method will return true if the sequence is found anywhere within the string and false otherwise.
One thing to watch out for with contains() is the potential for a NullPointerException. This will occur if your call contains() on a null string reference. So, we need to carefully check for nulls before using this method.
How does the Java contain () Method Works Internally?
Behind the scenes, the contains() method converts the input CharSequence parameter to a String if necessary. This allows you to pass objects like StringBuilder safely without any issues.
After this conversion, the method leverages the String class’s indexOf() method to determine if the sequence exists. More specifically, it checks if the return value from indexOf() is non-negative. A return value of 0 or greater indicates the index at which the sequence starts, meaning it was found.
In summary, the contains() method is mostly just a simple wrapper around indexOf() that returns a boolean value instead of an index.
Examples of Using Java String contains()
Let’s look at some examples to see the contains() method used in actual code.
Basic Usage
Here is a simple example that checks if a String contains another substring:
public class ContainsExample {
public static void main(String[] args) {
String str = "Hello World";
boolean contains = str.contains("World");
System.out.println(contains); // Prints true
}
}Output:
true
This demonstrates the primary usage – passing the sequence we want to find and getting a boolean result.
Case Sensitivity
One crucial point about contains() is that it performs a case-sensitive search:
public class ContainsExample {
public static void main(String[] args) {
String str = "Hello World";
boolean contains = str.contains("world");
System.out.println(contains); // Prints false
}
}Output:
False
So the case must match exactly, or contains() will return false.
Using contains() in Control Structures.
We can use the boolean result of contains() in control structures like if statements and loops:
public class ContainsExample {
public static void main(String[] args) {
String str = "Hello World";
if (str.contains("World")) {
System.out.println("World found!");
} else {
System.out.println("World not found!");
}
}
}Output:
World found!
This allows us to execute different logic depending on the result.
Limitations of contains() Method
The contains() method is very convenient for basic substring searches but does have some limitations:
- It is not well-suited for searching for individual characters. The indexOf() and lastIndexOf() methods are better for single char searches.
- It cannot tell us the index at which a sequence occurs – it only returns a boolean indicating if it was found. Again, indexOf() can provide the index.
So, while contains() is great for a simple existence check, you may want to use indexOf() instead for more advanced scenarios. It provides more flexibility and information about where in the string the match occurred.
Conclusion
The Java String contains() method, a simple yet powerful way to check if a substring exists within another string. Returning a boolean value can be easily incorporated into control flow and logic. However, contains() is case-sensitive, can trigger NullPointerExceptions, and has limitations compared to indexOf().
Overall, contains() provides an efficient mechanism for basic string search, but indexOf() may be required for more advanced use cases. With an awareness of its nuances, contains() can be leveraged to implement substring search logic in Java code cleanly.
We work very hard to provide you quality material
Could you take 15 seconds and share your happy experience on Google

