Java - String contains() Method



Description

The Java String contains() method is used to check whether the current string contains the specified sequence. It returns the Boolean value, which is true if and only if the string contains the sequence of char values; false otherwise.

The contains () method accepts a character sequence as an argument. It throws an exception if one of the sequence contains null value. A char sequence is readable sequence of the character values.

Syntax

Following is the syntax of the Java String contains() method −

 public boolean contains(CharSequence s) 

Parameters

  • s − This is the sequence to search for.

Return Value

This method returns true if this string contains s, else false.

Checking if String contains a CharSequence Example

If the given string value contains the specified character sequence value, the contains() method returns true.

In the following program, we are instantiating the string class with the value "Java Programming". Then, we are creating the char sequence with the value "Java". Using the contains() method, we are trying to check whether the given string contains the specified char sequence or not.

 package com.tutorialspoint; public class Concat { public static void main(String[] args) { //instantiate the string class String str1 = new String("Java Programming"); System.out.println("The given string: " + str1); //initialize the char sequence CharSequence str2 = "Java"; System.out.println("The given char sequence is: " + str2); //using the contains() method boolean bool = str1.contains(str2); System.out.println("The string contains the specified char sequence or not? " + bool); } } 

Output

On executing the above program, it will produce the following result −

 The given string: Java Programming The given char sequence is: Java The string contains the specified char sequence or not? true 

Checking if String is not containing a CharSequence Example

If the given string value does not contain the specified character sequence value, the contains () method returns false.

In the following program, we are creating an object the string class with the value "HelloWorld". Then, we are creating the char sequence with the value "helo". Using the contains() method, we are trying to check whether the given string contains the specified char sequence or not.

 package com.tutorialspoint; public class Concat { public static void main(String[] args) { // creating an object the string class String str1 = new String("HelloWorld"); System.out.println("The given string: " + str1); //initialize the char sequence CharSequence str2 = "helo"; System.out.println("The given char sequence is: " + str2); //using the contains() method boolean bool = str1.contains(str2); System.out.println("The string contains the specified char sequence or not? " + bool); } } 

Output

Following is the output of the above program −

 The given string: HelloWorld The given char sequence is: helo The string contains the specified char sequence or not? false 

Getting Exception while Checking if String contains a CharSequence Example

If the given char sequence value is null, this method throws a NullPointerException.

In this program, we are creating a string literal with the value "hello". Then, we are creating a char sequence with the null value. Using the contains() method, we are trying to check whether the given string contains the specified char sequence null or not.

 public class Concat { public static void main(String[] args) { try { // instantiate the string class String str1 = "hello"; System.out.println("The given string: " + str1); CharSequence str2 = null; System.out.println("The given char sequence is: " + str2); //using the contains() method boolean bool = str1.contains(str2); System.out.println("The string conatins the specified char seqience or not? " + bool); } catch(NullPointerException e) { e.printStackTrace(); System.out.println("Exception: " + e); } } } 

Output

The above program, produces the following output −

 The given string: hello The given char sequence is: null java.lang.NullPointerException: Cannot invoke "java.lang.CharSequence.toString()" because "s" is null	at java.base/java.lang.String.contains(String.java:2854)	at com.tutorialspoint.StringBuilder.Concat.main(Concat.java:11) Exception: java.lang.NullPointerException: Cannot invoke "java.lang.CharSequence.toString()" because "s" is null 

Checking if String contains a CharSequence in case sensitive mode Example

If the given string and char sequence value is the same, but the cases are different, the contains() method return false.

In this example, we are instantiating the string class with the value "TUTORIX". Then, we are creating the char sequence with the value "tutorix". Using the contains() method, we are trying to compare whether the string contains the specified char sequence or not.

 package com.tutorialspoint; public class Concat { public static void main(String[] args) { // instantiate the string class String str1 = new String("TUTORIX"); System.out.println("The given string: " + str1); CharSequence str2 = "tutorix"; System.out.println("The given char sequence is: " + str2); //using the contains() method boolean bool = str1.contains(str2); System.out.println("The contains() method return: " + bool); if(bool) { System.out.println("The given string contains the spcified char sequence"); } else { System.out.println("The given string does not contain the specified char sequence"); } } } 

Output

After executing the above program, it generates the following output −

 The given string: TUTORIX The given char sequence is: tutorix The contains () method return: false The given string does not contain the specified char sequence 
java_lang_string.htm
Advertisements