Java String contains() Method

You are Here:

Java String contains()

The contains() method checks whether the substring occurs within a given string or not.

Example

Java Compiler
public class myClass { public static void main(String[] args) { String str1 = "country"; String str2 = "try"; String str3 = "con"; System.out.println(str1.contains(str2)); System.out.println(str1.contains(str3)); } }

Output

true false

Syntax

public boolean contains(str2)

Parameter Values

ValueTypeExplanation
str2RequiredSpecifies the substring to search for.

Return Value

ValueExplanation
trueIf the specified substring occurs within the given string.
falseIf the specified substring does not occurs within the given string.
NullPointerExceptionIf the returned value is null.

Reminder

Hi Developers, we almost covered 90% of String functions and Interview Question on Java with examples for quick and easy learning.

We are working to cover every Single Concept in Java.

Please do google search for:

Join Our Channel

Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.

This channel is primarily useful for Full Stack Web Developer.

Share this Page

Meet the Author