.contains()
Published Jul 26, 2021Updated Sep 3, 2021
Contribute to Docs
Returns true
if a sequence of characters exists in a given string, otherwise false
.
Syntax
string.contains(CharSequence characters)
characters
(required): A character value that specifies the character(s) to be searched for.
Example 1
Check if the variable sentence
contains either the string "fox"
or "bear"
:
class AnimalFinder {public static void main(String[] args) {String sentence = "The quick brown fox jumps over the lazy dog.";System.out.println(sentence.contains("fox"));System.out.println(sentence.contains("bear"));// Output: true// Output: false}}
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Java on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Free course
Learn Java
Learn to code in Java — a robust programming language used to create software, web and mobile apps, and more.Beginner Friendly17 hours