contains() in Java

Learn via video course
FREE
View all courses
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Topics Covered

The contains() method is a feature of Java's String class. It's designed to seek out a specific sequence or set of characters within a given string. Notably, it's unable to be used to search for a single character, though we can delve into that further later on. This method returns a boolean value, returning true if the string contains the sought-after sequence of characters, and false otherwise.

Syntax of contains() in Java

contains() method belongs to the Java String class.

Syntax of this method is as follows:

In this context, "ch" represents the sequence of characters to be searched within a given string. Therefore, the "contains" method is invoked on the string to determine if it includes the specified sequence.

Parameters of contains() in Java

The contains() method accepts one parameter, which is the character sequence to be searched. This sequence can be in various forms such as a string, StringBuffer, etc. However, passing data types like int will result in an incompatible type error.

For example:

Return Values of contains() in Java

Return Type: boolean

Since contains() is a boolean method. It returns -

  • true: If sequence of characters is present in the string
  • false: If the sequence of characters is not present in the string.

Exceptions of contains() in Java

When the contains() method encounters a null value within the sequence of characters, it raises a NullPointerException.

Output:

The NullPointerException arises due to passing a null parameter to the method in the provided program.

Example

Let's consider a short example to see the use case of contains( ) method.

Output:

In the provided code snippet, there's a declaration of a string variable named str. The contains() method is utilized to verify whether certain character sequences are present within str. If the specified sequence exists within str, the method returns true; otherwise, it returns false.

The function returns true when given an empty string because, by definition, an empty string is a subset of any string.

What is contains() method in Java?

From the signature, it is visible that it has a boolean return type.

Firstly, within the method, a sequence of characters is converted into a string. Following this conversion, the method invokes the indexOf function. This function scans the string and returns either 0 or any number greater than 0 if the specified string is found within it; otherwise, it returns -1. Consequently, when the indexOf function yields a result of 0 or any positive number, the contains method evaluates to true; otherwise, it returns false.

More about the contains() method in Java

While the contains() method is handy for various tasks, it does come with limitations worth noting: -It's not suitable for searching single characters. -It doesn't provide the index of the searched string within the given string.

More Examples of String.contains() in Java

Example 1: Suppose you have a list of email addresses of the format "name""age"@xyz.com. You need to check which email belongs to James. Here, contains() method can be called on the email addresses, let us see how:

Output:

In the scenario we've discussed, we've examined two email addresses by employing a loop that utilizes the contains() method. This method is invoked on each element within a list, thereby examining every email address individually.

For one of the email addresses, the method returns true as it contains the desired name or character sequence. However, for the other email address, the method returns false since there's no matching character sequence identified. Consequently, we can effectively determine which email address corresponds to the specific user.

Example 2: Using contains() With if...else

Certainly! Let's reframe the text:

"Utilizing the contains method within an if-else block offers a powerful approach. Imagine a scenario where specific modifications or operations are required on strings containing a particular sequence of characters. In such cases, employing the contains method within an if-else block proves highly effective."

Output:

In the provided program, the presence of the target sequence "World" within the string has been verified. As it exists within the string, the intended string operation has been executed. Specifically, the concat method has been utilized to append strings.

Example 3: Using case insensitive check

The contains() method, by default, is case-sensitive. However, with a simple adjustment, we can make it case-insensitive. One approach is to utilize either the toLowerCase() or toUpperCase() method. This involves converting both the string and the sequence of characters we're checking to either all lowercase or all uppercase. By doing so, we ensure a uniform comparison, allowing for easy detection regardless of the case..

Output:

In the example provided, we've utilized lowercase and uppercase transformations on both the sequence and the string to facilitate comparison.

Example 4: Contains() with single character

Output:

In the provided code snippet, an error occurred when using the contains method with a single character 'S'. This suggests that the contains method cannot be utilized for individual characters.

Conclusion

  • contains () method is a built-in Java method that helps us check if a sequence of characters is present inside a given string or not.
  • The return type of this method is boolean, thus returning true or false.
  • It takes a single parameter,i.e., the sequence of characters to be searched.
  • Major Application of this method includes verification tasks like checking if a string is a substring of the other.