Latest Code Tutorials

Java String Contains Example | String contains() in Java


Java string contains() is an inbuilt method that was introduced in Java 1.5 release and it is used to find the sequence of characters in a given string. If that sequence is found it returns true, otherwise it returns false. Java String contains() function is used to check if the string contains the particular character sequence or not.

Java String Contains Example

Java string contains() method searches the sequence of characters in this string. It returns true if a sequence of char values are found in this string otherwise returns false.


public boolean contains(CharSequence sequence)

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

Implementation of this Method

The contains() method internally uses indexOf method, below the refers how to implement it:

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

The contains() method internally uses String indexOf method, below code snippet, shows the contains() method definition.

Here conversion of CharSequence to a String takes place, and then the indexOf method is called which returns 0 or higher value if it finds the string. Otherwise, it returns -1.

Exception in this method

This method usually throws Null Pointer Exception if it finds that the length of the string is NULL.

Example of the string contains()

See the following code.

class contains {
    public static void main(String args[]) {
        String s1 = "Welcome to Appdividend";
        // prints true
        // prints false

See the output.

String contains() Method in Java

Now we will see one example of case sensitive method because contains() method is case sensitive.

See the following code.

class contains {
    public static void main(String args[]) {
        String s1 = "Welcome to Appdividend";

        // prints true

        // prints false

See the output.

Java String Contains

When to use Contains() method

It is the common case in programming when you want to check if the specific String contains the particular substring. For example, If you’re going to test if the String “The big red fox” contains the substring “red.”

This method is useful in such a situation.

See the following Java String contains() method in the if-else Loop.

public class IfExample {
    public static void main(String args[]) {
        String str1 = "Java string contains If else Example";
        // In If-else statements you can use the contains() method

        if (str1.contains("example")) {
            System.out.println("The Keyword :example: is found in given string");
        } else {
            System.out.println("The Keyword :example: is not found in the string");

Java String contains() with case insensitive check

In the case of an insensitive check, we can change both the strings to either upper case or lower case before calling the contents() function.



See the output.


Java String contains() with CharSequence

Let’s see some examples of using String contains() method with StringBuffer, StringBuilder and, CharBuffer. All these are implementations of the CharSequence interface.

Also, note that CharBuffer is an abstract class, creating its instance is slightly different from other usual classes.

StringBuffer sb = new StringBuffer("User");
System.out.println(str.contains(sb)); // true

StringBuilder sb1 = new StringBuilder(sb);
System.out.println(str.contains(sb1)); // true

CharBuffer cb = CharBuffer.allocate(4);
System.out.println(cb); //User
System.out.println(str.contains(cb)); // true

Finally, the String contains() method in Java is over.

Recommended Posts

ArrayList in Java Tutorial

Java Enum Tutorial With Example

Java Multithreading Tutorial

Object-Oriented Programming in Java

Inheritance in Java

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.