java.lang.String.contains() method searches the sequence of characters in the given string. It returns true if sequence of char values are found in this string otherwise returns false.
Implementation of this method :
public boolean contains(CharSequence sequence)
{
return indexOf(sequence.toString()) > -1;
}
Here convertion of CharSequence to a String takes place and then indexOf method is called. The method indexOf returns O or higher number if it finds the String, otherwise -1 is returned. So, after execution, contains() method returns true if sequence of char value exists, otherwise false.
Syntax :
public boolean contains(CharSequence sequence) Parameter : sequence : This is the sequence of characters to be searched. Exception : NullPointerException : If seq is null
Example : To check whether the charSequence is present or not.
// Java program to demonstrate working // contains() method class Gfg { // Driver code public static void main(String args[]) { String s1 = "My name is GFG"; // prints true System.out.println(s1.contains("GFG")); // prints false System.out.println(s1.contains("geeks")); } } |
true false
Example : Case sensitive method to check whether given CharSequence is present or not.
// Java code to demonstrate case // sensitivity of contains() method class Gfg1 { // Driver code public static void main(String args[]) { String s1 = "Welcome! to GFG"; // prints false System.out.println(s1.contains("Gfg")); // prints true System.out.println(s1.contains("GFG")); } } |
false true
Further Thoughts :
- This method does not work to search for a character.
- This method does not find index of string if it is not present.
- For above two functionalities, there is a better function String indexOf
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Collator compare(String, String) method in Java with Example
- Collator equals(String, String) method in Java with Example
- Java.lang.string.replace() method in Java
- Java String trim() method with Example
- Java String charAt() method with example
- Java String isEmpty() method with example
- Java.lang.String.lastIndexOf() Method
- ZoneOffset of(String) method in Java with Examples
- RuleBasedCollator getCollationElementIterator(String) method in Java
- String toString() Method in java with Examples
- Java String subSequence() method with Examples
- CollationElementIterator setText(String) method in Java with Example
- Split() String method in Java with examples
- PrintWriter println(String) method in Java with Examples
- Logger info(String) method in Java with Examples
- StringWriter write(String, int, int) method in Java with Examples
- DecimalFormatSymbols setInfinity(String) method in Java with Examples
- BreakIterator setText(String) method in Java with Examples
- DecimalFormatSymbols setNaN(String) method in Java with Examples
- Matcher group(String) method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : bhaktiramnan

