Java String contains()

Last Updated On Wednesday 29th Jun 2022

java string contains

The contains() method checks whether the specified string is present in the string or not.

Syntax

	string.contains(CharSequence strName)
	

Parameters

  • strName – sequence of characters ( charSequence )

Note: A charSequence is a sequence of characters such as: String, CharBuffer, StringBuffer etc.

Return Value

  • returns true – if the string contains the specified character
  • returns false – if the string doesn’t contain the specified character
	String myStr = "Codejagd";
System.out.println(myStr.contains("code"));  // true
System.out.println(myStr.contains("ja"));  // true
System.out.println(myStr.contains("Hw")); // false
	

contains java

	class Main {
  public static void main(String[] args) {
    String str1 = "Web Development";
    Boolean result;

    result = str1.contains("Web");
    System.out.println(result);

    result = str1.contains("Dev");
    System.out.println(result);

    result = str1.contains("App");
    System.out.println(result);

    result = str1.contains("");
    System.out.println(result);
  }
}
	

Output

	true
true
false
true
	

Implementation

Implementation of this method

	public boolean contains(CharSequence seq) {
   return indexOf(seq.toString()) > -1;
}
	
  • The method indexOf returns 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.
java contains
contains java
java contains
contains in java
contains string java
contains method in java
contains method in java