Table of contents
1.
Introduction
2.
String contains() Method in Java
3.
Syntax of contains() method
4.
Parameter of contains() method
5.
Exception of contains() method
6.
Returns Value of contains() method
7.
Implementation of contains() method
8.
Example of the java.string.contains() method
8.1.
Example 1: To check whether the charSequence is present or not. 
8.2.
Example 2: Case-sensitive method to check whether given CharSequence is present or not. 
9.
Limitations of Java string contains() method
10.
Frequently Asked Questions
10.1.
How do I check if a string contains a specific sequence of characters?
10.2.
How do you compare strings in Java?
10.3.
How do I find a word in a string in Java?
10.4.
What does string () do in Java?
11.
Conclusion
Last Updated: Oct 8, 2024
Easy

Java String contains() Method

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In Java, strings are objects representing a sequence of character values. An array of characters work the same as a string in Java. The Java String class has a lot of methods to perform operations on strings like compare(), concat(), equals(), split(), length(), replace(), compareTo(), contains(). The string contains() method is used when you need to check whether a string contains a certain sequence of characters or not. In this article, we will learn about the string contains() method with the help of code examples. Let us dive into the topic.

java string contains method

String contains() Method in Java

The String.contains() method is used to search a sequence of characters in the given string. If the sequence of char values is found in this string, it returns true, otherwise returns false. 

Syntax of contains() method

public boolean contains(CharSequence sequence)    

Parameter of contains() method

sequence: It specifies the sequence of characters to be searched.

Exception of contains() method

NullPointerException: If the sequence is null

Returns Value of contains() method

true if the sequence of char values exist; otherwise, false.

Implementation of contains() method

public boolean contains(CharSequence sequence)
{
   return indexOf(sequence.toString()) > -1;
}

In this case, CharSequence is converted to a String and then indexOf is called. If indexOf finds the String, it returns O or a higher number, otherwise -1. In other words, if a sequence of char values exists, the contains() method returns true, otherwise it returns false. 

Example of the java.string.contains() method

Let us look over a few examples to understand the concepts better.

Example 1: To check whether the charSequence is present or not. 

class Ninja1 {
    public static void main(String args[]) {
        String var1 = "My company's name is CodingNinjas";
        
        // Checking if "CodingNinjas" is present in var1
        System.out.println(var1.contains("CodingNinjas")); // This will print true
        
        // Declare and initialize a new string for another check
        String s1 = "CN";
        System.out.println(var1.contains(s1)); // This will print false since "CN" is not a part of var1
    }
}
You can also try this code with Online Java Compiler
Run Code

 

Output: 

true
false

Example 2: Case-sensitive method to check whether given CharSequence is present or not. 

class Ninja2 {
    public static void main(String args[]) {
        String var2 = "CodingNinjas";
        
        // Checking if 'var2' contains "Ninjas"
        System.out.println(var2.contains("Ninjas")); // This will print true
        
        // Checking if 'var2' contains "NINJAS" (case-sensitive check)
        System.out.println(var2.contains("NINJAS")); // This will print false
    }
}
You can also try this code with Online Java Compiler
Run Code

 

Output: 

true
false

Limitations of Java string contains() method

  • Case Sensitivity: The method is case-sensitive, making it unable to perform case-insensitive searches directly.
  • No Regular Expression Support: contains() only checks for literal substrings and does not support regex.
  • Performance with Large Strings: It can be inefficient with large strings, especially when used frequently, due to its linear time complexity.
  • Null Handling: Passing a null argument to contains() will result in a NullPointerException, requiring manual null checks.
  • No Index Information: It only returns a boolean, without indicating the position of the substring in the string.
  • Immutable Strings: Since Java strings are immutable, contains() cannot modify the string; it only checks for the substring's presence.

Frequently Asked Questions

How do I check if a string contains a specific sequence of characters?

The String.contains() method is used to search the sequence of characters in the given string. If the sequence of char values is found in this string, it returns true, otherwise returns false. 

How do you compare strings in Java?

The equals() method is used to compare two strings in java.

How do I find a word in a string in Java?

To find a word in the string, we are using indexOf() and contains() methods of String class. The indexOf() method is used to find an index of the specified substring in the present string.

What does string () do in Java?

In Java, String() is a constructor that creates a new String object. It can initialize a string from various sources, such as character arrays, byte arrays, or another string. While strings are usually created using literals, the String() constructor allows for more flexible and explicit string creation.

Conclusion

The Java String contains() Method is a straightforward and effective tool for checking the presence of a substring within a string. While it is case-sensitive and lacks regex support, it serves well for simple substring checks. However, for complex searches or large datasets, consider its limitations and explore alternative methods to optimize performance and functionality in your Java applications.

Also read:

 

We hope that this blog has helped you enhance your knowledge, and if you wish to learn more, here are some courses provided by Coding Ninjas: Basics of C++ with DSACompetitive Programming and MERN Stack Web Development

Live masterclass