w3resource logo


Introduction to Java Programming

Java String: contains() Method

Secondary Nav

public boolean contains(CharSequence s)

The contains() method returns true if and only if this string contains the specified sequence of char values.

Java Platform: Java SE 8

Syntax :

contains(CharSequence s)

Parameters:

Name Description Type
s the sequence to search for String

Return Value : true if this string contains the specified sequence of char values, false otherwise.

Return Value Type: boolean

Example : Java String contains() Method

public class Example {

public static void main(String[] args)
    {
        String str1 = "Java Exercises and SQL Exercises";
        String str2 = "and";
System.out.println();
System.out.println("Original String: " + str1);
System.out.println("Specified sequence of char values: " + str2);
System.out.println(str1.contains(str2));
System.out.println();
    }
}

Output:

Original String: Java Exercises and SQL Exercises      
Specified sequence of char values: and                 
true

Java Code Editor:



Join our Question Answer community to learn and share your programming knowledge.