Java Stringcontains() Method
Example
Find out if a string contains a sequence of characters:
String myStr = "Hello";System.out.println(myStr.contains("Hel")); // trueSystem.out.println(myStr.contains("e")); // trueSystem.out.println(myStr.contains("Hi")); // falseDefinition and Usage
Thecontains() method checks whether a string contains a sequence of characters.
Returnstrue if the characters exist andfalse if not.
Syntax
public boolean contains(CharSequencechars)Parameter Values
| Parameter | Description |
|---|---|
| CharSequencechars | 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: | Aboolean, 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

