contains() in Java | contains() Function in Java – Scaler Topics

contains() method is a part of the Java String class. It searches for a sequence or set of characters in a given string (note that it can not be used for a single character, we shall discuss it more later). It has a boolean return type, i.e., true is returned if the string contains the sequence of characters else false.

Here, ch represents the sequence of characters to be searched. Hence, the contains method is called on the given string.

Syntax of this method is as follows:

Here, ABC is the sequence of characters taken as the parameter.

contains() method takes a single parameter. It is the sequence of characters that is to be searched. It can be a sequence of characters like string, StringBuffer, etc. Other datatypes like int shall result in an incompatible type error.

In the above Program, string str is declared. The method contains() has been used to check if the character sequences are a substring of str or not. If yes, true is returned, and false otherwise.

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

In the above program, NullPointerException occurs since we have passed null as a parameter for the method.

The contains() method throws NullPointerException in case the value of the sequence of characters is null.

Note that for an empty string, true is returned since it is a subset of every string.

What is

contains()

method in Java?

Let’s discuss how the contains() method works internally and its signature.

The signature of contains () method is as follows:

public

boolean

contains

(CharSequence sequence)

From the signature, it is visible that it has a boolean return type. Let’s dive in and look at its internal implementation. (This is just for the sake of understanding, don’t worry much about the implementation)

public

boolean

contains

(CharSequence sequence)

{

return

indexOf(sequence.toString()) > -

1

;

}

Inside the method, first of all, the conversion of a sequence of characters to a string occurs. Further, the indexOf method is called. This method returns 0 or any higher number if it finds the string; otherwise, it returns -1. So once it is executed, the contains method returns true when the indexOf method returns 0 or any higher number; otherwise, it returns false.

More about the

contains()

method in Java

We have seen the use cases of contains() method. However, there are some limitations that need to be considered.

  • This method can not be used for searching a single character.
  • This method can not be used to find the index of the searched string in the given string.

More Examples of

String.contains()

in Java

Example 1: Java String contains()

Now that we have covered the basics of contains() let’s look at an example. 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:

class

Main

{

public

static

void

main

(String[] args)

{

String str1 =

"[email protected]"

;

String str2 =

"[email protected]"

Boolean result;

result = str1.contains(

"James"

);

// here str1 contains "James"

System.out.println(result);

// true

result = str2.contains(

"James"

);

// no char sequence for "James"

System.out.println(result);

//false

} }

Output:

true

false

(For the example we have just considered two emails, we can have a list of the same and using a loop contains method can be called on every element of the list i.e., every email address).
So here we have called the contains() method on two strings representing two email addresses. One with the desired Name or string of characters (which returns true). For the other string, the method returns false since no matching character sequence is found. Hence, we know which email address belongs to the particular user.

Example 2: Using contains() With if…else

We can use the method inside an if – else block as well. Let us look at the implementation. Consider a case where you need to perform some modifications or operations on the strings containing a particular sequence of characters. In this case, contains method with if-else block can be used. Let us see how

class

Main

{

public

static

void

main

(String[] args)

{

String str1 =

"Hello World! Let us learn Java."

;

Boolean result;

// using contains inside if, as boolean condition

if

(str1.contains(

"World"

)) {

System.out.println(

"The required string is: "

.concat(str1));

}

else

{

System.out.println(

"Operation can not be performed."

);

} } }

Output:

The required string is: Hello World! Let us learn Java.

In the above program, we have checked if the string contains the desired sequence of characters or not, “World” in this case. Since here it does. Thus we have performed the desired string operation. Here, the concat method is used to append strings.

Example 3: Using case insensitive check

contains() method is case sensitive, however with some modifications we can use this method for case insensitive check.

Basically we can take help of toLowerCase() or toUpperCase() method. This way, both the string and sequence of characters are converted to lower case or upper case and can easily be checked. Below is the implementation.

class

Example

{

public

static

void

main

(String args[])

{

String str =

"HELLO World! Let us learn Java."

;

String str1 =

"hello"

;

// converting strings to the same case and then using contains method

System.out.println(str.toLowerCase().contains(str1.toLowerCase())); System.out.println(str.toUpperCase().contains(str1.toUpperCase())); } }

Output:

true

true

In the above example, we have converted both the sequence and string to lowercase and uppercase for checking.

Example 4: Contains() with single character

class

Main

{

public

static

void

main

(String[] args)

{

String str =

"Java String Contains() Tutorial"

;

// passing sigle character as a parameter

boolean

result = str.contains(

'S'

);

System.out.println(result); } }

Output:

javac /tmp/Mosnlruyve/Main.java

/tmp/Mosnlruyve/Main.java:

5

: error: incompatible types:

char

cannot be converted to CharSequence

boolean

result = str.contains(

'S'

);

^ Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output

1

error

In the above program, We used contains method for a single character ‘S’. However, this results in an error. Hence, contains method can not be used for a single character.