×

Java String contains() method

contains() method returns true only if it contains the given sequence of characters otherwise it returns false.

syntax:

public boolean contains(CharSequence sequence)

parameters:

sequence : It is the sequence to be searched.

Returns:

It returns true when specified sequence character is found, Otherwise it returns false

Throws:

NullPointerException :when the sequence of characters is not found.

Example 1:

   public class ContainsEx1
{ 
public static void main(String args[])
{ 
  String name="tutorial and example"; 
  System.out.println(name.contains("tutorial")); 
  System.out.println(name.contains("and  "));
  System.out.println(name.contains("and"));
  System.out.println(name.contains("guide"));
  System.out.println(name.contains("example"));
  System.out.println(name.contains(" "));
     }
}

Output:

true                
false                
true              
false                
true               
true

Example 2: Case Sensitive

public class ContainsEx2
{ 
    public static void main(String[] args)
{ 
        String str = "Tutorial And Example World"; 
        boolean isContains = str.contains("World"); 
        System.out.println(isContains);
        if(str.contains("And1")){
            System.out.println("Welcome to the Java Tutorial and Example");    
          }else{
              System.out.println("J -->"+str.contains("And1"));
          } 
         System.out.println(str.contains("Tutorial And Example")); 
        // Case Sensitive 
        System.out.println(str.contains("Tutorial and Example")); // false 
    } 
}

Output:

true
J-->false
true
false

Example 3: Case Insensitive

public class ContainsEx3 {
    public static void main(String[] args) {
        String str = "tutorialandexample world";
        String str1 = "TUToRIalaNdExAmPLe WOrLd";
        System.out.println("Case Check: " +   str.contains(str1.toLowerCase()));
    }
}

Output:

 Case Check: true

Related Topics

Moran Numbers in Java

In this article, we will be acknowledged about the moran numbers in Java, how they are formed, what are the approaches to achieve the moran numbers. Moran Number A moran numbers are...

3 minutes read.

Java For Keyword

For as a keyword in java: When we need to run a set of statements repeatedly in Java, we use loops. The Java for loop offers a clear way to express...

4 minutes read.

Java Integer compareTo() method

The compareTo() method of Integer class compares two Integer objects numerically. Syntax public static int compareTo(int anotherInteger) Parameters The parameter ‘anotherInteger’ represents the Integer to be compared. Specified by This method is specified by compareTo in...

2 minutes read.

JDK | Java Development Kit

Java Development Kit (JDK) The Java Development Kit is a software development environment used to create Java applications. The JDK includes JRE (Java Runtime Environment), an interpreter (java), a compiler (javac),...

3 minutes read.

Mutable class in Java

A language for object-oriented programming is Java. Because this is an object-oriented language of programming, all of its mechanisms and methods are based on objects. Java has a concept of...

6 minutes read.

How to Develop Programming Logic in Java?

Introduction In the world of software development, Java programming language is one of the most powerful programming languages that is used to create a wide range of applications. It includes desktop,...

18 minutes read.

Java Garbage Collection

Java Garbage Collection In Java, unreferenced objects are treated like garbage. The process of reclaiming the unused memory during runtime automatically is known as the Java Garbage Collection.In other words, the...

4 minutes read.

Java New Keyword

To create a class instance in Java, use the new keyword. In other words, it returns a reference to the memory that was allocated for a new object and instantiates...

3 minutes read.

How to create a mirror image of a 2D array in Java

Problem Statement We have provided a list of m x n. here m indicates rows, and n indicates columns). Printing the fresh matrices should result in a mirror reflection of an...

2 minutes read.

Java calculate age

In this section, we will create a Java program that calculates age from the given date of birth or current date. In order to get the date of birth from the current...

6 minutes read.

How to Set Java_home in Linux

To set the Java_home in Linux, we must follow several steps to make us understand it easily.  Java follows the principle called WORA (write once run anywhere). We know that java...

3 minutes read.

Java Math min() Method

The min() method of Math class returns the smaller of two arguments. The arguments can be of double, float, int or long data type. Syntax: public static double min (double a, double...

2 minutes read.

Convert List to String in Java

We occasionally need to create a string from a list of characters. Since a string is only a collection of characters, a character array can be converted into a string....

6 minutes read.

Java String replaceFirst() method

This method replace the first substring with user specified String. Syntax: public String replaceFirst(String Regexp, String Replacement) Parameter: Regexp : Regular Expression Replacement : the String which would replace found expression Return: a string Java String replaceFirst()...

1 minute read.

Math fma () method in java

In java, the Math module constitutes of fma () Method in it. This method can be accessed in two ways which can be differentiated by the parameters which are given...

4 minutes read.

Java delete directory

The File classes in Java may symbolize a directory or a file on the system. Inside the java.io package, the Files class is accessible. The File class has several helpful...

2 minutes read.

Short Circuit Logical Operators in Java

When there are two or more relational expressions in a decision-making statement, logical operators are utilized to combine them. The logical operators short circuit and not-short circuit fall into two...

5 minutes read.

Constructor Chaining and Constructor Overloading in Java

Constructor Chaining Constructor chaining and constructor overloading are two confusing terms. Let's first understand constructor chaining.Constructor chaining is the process of calling one constructor from another constructor using the same object....

2 minutes read.

Heart Pattern in Java

Heart Pattern is yet another intricate pattern program, however, due to its complexity, interviewers hardly ever inquire about it. Method for Printing the Heart Number Pattern Put the value of the total row...

2 minutes read.

House Numbers in Java

In this section, we will discuss about house number in Java. It is a sum of cubes, each of which has a dimension of h + 1. There is a...

3 minutes read.