Java 25 (LTS) reference · String Methods
Java String contains() Method
Return true if the string contains a sequence of characters. Case-sensitive.
Java 25 (LTS) Since Java 5
Syntax
string.contains(sequence)
Parameters
| Parameter | Description |
|---|---|
sequenceCharSequence |
The text to look for. |
Return value
true or false.
Examples
public class Main {
public static void main(String[] args) {
String log = "ERROR: disk full";
System.out.println(log.contains("ERROR"));
System.out.println(log.contains("error"));
}
}
Output
true false
Run this on your computer — see “Get Started” for setup.
public class Main {
public static void main(String[] args) {
String text = "Learn Java Streams";
System.out.println(text.toLowerCase().contains("java"));
}
}
Output
true
Run this on your computer — see “Get Started” for setup.
Version history
| Version | Change |
|---|---|
| Java Java 5 | Added |
Related
string.indexOf()· Return the index of the first occurrence of a character or substring, or -1 if it isn't found.string.startsWith()· Return true if the string begins with a prefix. endsWith() checks the end the same way.- Java Strings · tutorial