AppDividend
Latest Code Tutorials

Java String Contains Example | String contains() Method in Java

0

Java String Contains Example | String contains() method in Java is today’s topic. Java contains() method was introduced in Java 1.5 release. 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

The 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.

Syntax

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
        System.out.println(s1.contains("Welcome"));
        // prints false
        System.out.println(s1.contains("Hello"));
    }
}

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
        System.out.println(s1.contains("Appdividend"));

        // prints false
        System.out.println(s1.contains("appdividend"));
    }
}

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 insensitive check, we can change both the strings to either upper case or lower case before calling the contents() function.

"eleven".toLowerCase().contains("E".toLowerCase());

"eleven".toUpperCase().contains("E".toUpperCase());

See the output.

true
true

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);
cb.append('U');cb.append('s');cb.append('e');cb.append('r');
cb.clear();
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.