String contains() Method in Java

The string contains() Method in Java | This page explains the contains() method of the String class. We will also see some codes demonstrating contains a method(). The contains() method in the String class is used to search the substring in the string, if the substring is present it returns true or else false therefore the return type of contains is boolean. Observe the below examples:-

1) string = “How are you?”
string.contains(“How”)
Result = true

2) string = “How are you?”
string.contains(“how”)
Result = false
As the contains() method is case sensitive it returned false.

The method details of contains() method are as follows:-
public boolean contains(CharSequence ch)

Return type:- boolean (true or false)
Parameters:- ch- the substring or character needed to be searched.
Throws:- NullPointerException, if the charSequence is null.

The internal implementation of contains() method is as follows:-

public boolean contains(CharSequence s) {
   return indexOf(s.toString()) >= 0;
}

Java String Contains Method Example

Let us see the demonstration of the String contains method.

public class Main {
   public static void main(String args[]) {
      String name = "Java Programming Languge";
      System.out.println(name.contains("java"));
      System.out.println(name.contains("Java"));
      System.out.println(name.contains("Language"));
   }
}

Output:-

false
true
false

Example-2:- Java string method contains demonstration

public class Main {
   public static void main(String[] args) {
      String str = "Hello Java Readers";
      boolean isContains = str.contains("Java");
      System.out.println(isContains);
      System.out.println(str.contains("java"));
      System.out.println(str.contains("the"));
   }
}

Output:

true
false
false

Example-3:- Demonstration of String contains method in Java.

public class Main {
   public static void main(String[] args) {
      String text = "Java Programming Language";
      String word = "Programming"; 
      if (text.contains(word)) {
         System.out.println("String contains " + word);
      } else {
         System.out.println("String doesn't contains " + word);
      }
   }
}

Output:

String contains Programming

Example-4:- Demonstration of String contains method in Java.

This code can demonstrate in which case contains() method of the Java String class throws the NullPointerException.

public class Main {
   public static void main(String argvs[]) {
      String str = "Welcome to Know Program!";
      if (str.contains(null)) {
         System.out.println("Inside the if block");
      } else {
         System.out.println("Inside the else block");
      }
   }
}

Output:-

Exception in thread “main” java.lang.NullPointerException: Cannot invoke “java.lang.CharSequence.toString()” because “s” is null
at java.base/java.lang.String.contains(String.java:2054)
at Main.main(Main.java:4)

public class Main {
   public static void main(String argvs[]) {
      String str = null;
      if (str.contains(" ")) {
         System.out.println("Inside the if block");
      } else {
         System.out.println("Inside the else block");
      }
   }
}

Output:-

Exception in thread “main” java.lang.NullPointerException: Cannot invoke “String.contains(java.lang.CharSequence)” because “str” is null
at Main.main(Main.java:4)

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *