w3resource

Java String: contains() Method

contains() method - Checks whether a string contains another string

The contains() method is used to check if and only if a string contains the specified sequence of character values.

Pictorial presentation of Java String contains() Method

Java String: contains() Method

Java Platform: Java SE 8 and above

Syntax contains() method

contains(CharSequence s)

Parameters contains() method

Name Description Type
s The sequence to search for. String

Return Value contains() method:

  • Returns true if the string contains the specified sequence of character 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:

Previous:concat Method
Next:contentEquals Method



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/java-tutorial/string/string_contains.php