What is the String.contains() method in Java?

Share

The contains() method in Java is used to search the substring in a given String. Returns: If the substring is found in the specified String, then it returns a true value; otherwise, it returns​ a false value.

Syntax

public boolean String.contains(CharSequence substring)
svg viewer

Code

The same example that was shown above is given in the code snippet below. This method is case-sensitive.

class HelloWorld {
public static void main( String args[] ) {
String str = "Hello World";
// prints true
System.out.println(str.contains("World"));
// prints false
System.out.println(str.contains("world"));
}
}
Copyright ©2024 Educative, Inc. All rights reserved