Java String contains() Method
Example
Find out if a string contains a sequence of characters:
String myStr = "Hello";
System.out.println(myStr.contains("Hel")); // true
System.out.println(myStr.contains("e")); // true
System.out.println(myStr.contains("Hi")); // false
Definition and Usage
The contains()
method checks whether a string contains a sequence of characters.
Returns true
if the characters exist and false
if not.
Syntax
public boolean contains(CharSequence chars)
Parameter Values
Parameter | Description |
---|---|
CharSequence chars | The characters to be searched for |
The CharSequence interface is a readable sequence of char values, found in the java.lang package.
Technical Details
Returns: | A boolean , indicating whether a sequence
of characters exist in the specified string:
|
---|---|
Throws: | NullPointerException - if the returned value is null |
Java Version: | 1.5 |
❮ String Methods