Curriculum
Course: Java Basic
Login

Curriculum

Java Basic

Java Home

0/1

Java Introduction

0/1

Java Get Started

0/1

Java Syntax

0/1

Java Comments

0/1

Java Type Casting

0/1

Java Operators

0/1

Java Booleans

0/1

Java Switch

0/1

Java Break / Continue

0/1

Java Errors and Exception

0/1
Text lesson

contains()

Example

Determine whether a string includes a particular sequence of characters.

String myStr = "Hello";
System.out.println(myStr.contains("Hel"));   // true
System.out.println(myStr.contains("e"));     // true
System.out.println(myStr.contains("Hi"));    // false

Definition and Usage

The contains() method verifies if a string includes a specific sequence of characters.

It returns true if the characters are found, and false otherwise.

Syntax

public boolean contains(CharSequence chars)

Parameter Values

Parameter

Description

CharSequence chars

The characters to search for in the string.

 

The CharSequence interface is a readable sequence of char values, found in the java.lang package.

Technical Details

Returns:

A boolean indicating whether a sequence of characters exists in the specified string:

  • true: The sequence of characters exists
  • false: The sequence of characters does not exist

Throws:

NullPointerException – if the returned value is null

Java Version:

1.5