Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Last Updated: Mar 27, 2024
Difficulty: Easy

String contains()

Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

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.

String contains()

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

public boolean contains(CharSequence sequence)    

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

Exception
NullPointerException: If the sequence is null

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

Implementation of this 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. 

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Examples

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";
        System.out.println(var.contains("CodingNinjas"));
        System.out.println(s1.contains("CN"));
    }
}

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";
        System.out.println(var2.contains("Ninjas"));
        System.out.println(s1.contains("NINJAS"));
    }
}

Output: 

true
false

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.

Conclusion

In this article, we have extensively discussed the string contains() method with the help of code examples. Having gone through this article, I am sure you must be excited to read similar blogs. Coding Ninjas has got you covered. Here are some similar blogs to redirect: Converting Java strings to intUnderstanding strings in javaMethod to take input in javaint to string conversion in java  We hope that this blog has helped you enhance your knowledge, and if you wish to learn more, check out our Coding Ninjas Blog site and visit our Library. Here are some courses provided by Coding Ninjas: Basics of C++ with DSACompetitive Programming and MERN Stack Web Development. Do upvote our blog to help other ninjas grow.

Happy Learning!

Topics covered
1.
Introduction
2.
String contains()
3.
Examples
3.1.
Example 1 
3.2.
Example 2 
4.
Frequently Asked Questions
4.1.
How do I check if a string contains a specific sequence of characters?
4.2.
How do you compare strings in Java?
4.3.
How do I find a word in a string in Java?
5.
Conclusion