In this section we will learn what the String contains() method is and how to use it in Java.
What is Java String contains() Method?
When we want to check a string value to see if it contains a specific sequence of characters in it, we can use the contains() method.
Java contains() Method Syntax:
public boolean contains(CharSequence chars)
contains() Method Parameters:
The `contains()` method takes a string as its argument and checks the target string to see if it contains the character sequence that we set as the argument of the method.
Note: The `CharSequence` is an interface and the `String` class implemented it. So we can use String objects as the argument to this method.
contains() Method Return Value:
The return value of this method is of type boolean:
The method returns true of the target string contained the character sequence and false otherwise.
Example: using String contains() method
public class Simple { public static void main(String[] args) { String s = "Life is good"; System.out.println(s.contains("Life")); System.out.println(s.contains("is not good")); System.out.println(s.contains("is good")); } }
Output:
true
false
true