×

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

Reserved Keywords in Java

In Java, a reserved term is used as a code key called a keyword. Because they are predefined, these terms cannot be used for anything else. They cannot serve as...

3 minutes read.

IdentityHashMap in Java

The IdentityHashMap class is comparable to the HashMap class and is an AbstractMap implementation. However, when comparing the key, it uses reference equality rather than object equality (or values). Identity HashMap...

6 minutes read.

Encapsulation Program in Java

Encapsulation Program in Java Encapsulation program in Java demonstrates the technique to bind methods and fields in a single unit. The term encapsulation is inspired by the word ‘capsule’, which is...

3 minutes read.

How to check Date Null in Java?

In this section, we will be acknowledged about Date Null in Java. The date null in Java is an entity that is used when there is no specified value for...

3 minutes read.

Differences between Byte Code and Machine Code

This tutorial will discuss the differences between Byte Code and Machine Code. Before that, let's try to know what byte and machine code is. Introduction Every computer has a set of instructions...

4 minutes read.

Java HashMap

Java HashMap extends AbstractMap and implements Map interface. It is the collection of multiple entries where an entry consists of key and value pair. The HashMap can contain only one...

7 minutes read.

How to compare characters in Java

In this tutorial, we will learn about how to compare characters in Java. To compare characters in Java, we will learn about what is a character in Java Char The character is...

4 minutes read.

Java Lock

A lock is indeed a threaded synchronization technique similar to Java's synchronized blocks, however, locking can be more complex. It's not like we can completely get rid of the synchronized...

5 minutes read.

Armstrong Number Program in Java

Armstrong Number Program in Java: A positive number is called an Armstrong number if the sum of the cube of each digit is equal to the number itself. There are...

6 minutes read.

GCD Program in Java

GCD Program in Java The GCD program in Java outputs the GCD of the given numbers. In mathematics, Greatest Common Divisor (GCD), Greatest Common Factor or Highest Common Factor (HCF) of...

14 minutes read.

Economical number in Java

Economical number is said to be economically efficient if the number of digits after prime factorization of the original number with their powers is less than the number of digits...

3 minutes read.

Shopping Bill in Java

Java Shopping Bill Here in this program, we are about to create a JAVA class called Products which will have some properties or attributes like prod_name (Product Name ), qty (quantity),...

4 minutes read.

Difference Between Data Hiding and Abstraction in Java

Abstraction: Data Abstraction and Data Hiding ideas are utilised to show the expected data to the end client and conceal the superfluous subtleties, however, for specific purposes like decreasing the framework's...

10 minutes read.

Crown Pattern in Java

We know the importance of solving pattern problems. We can solve pattern problems by using any programming language. There is no rule to translating into a particular programming language. We...

4 minutes read.

Magnanimous Number in java

Magnanimous Number When the left and right halves of a majestic number are combined, the result is invariably a prime number, which must have at least two digits. The number's left...

3 minutes read.

User Defined Exception in Java

An exception is an error (run time error) that happened while a program was being executed. The program stops abruptly whenever the Exception occurs, and the code after the line...

3 minutes read.

Java Integer compare() method

The compare() method of Integer class compares the two specified int values. Syntax public static int compare(int x, int y) Parameters The parameters ‘x’ and ‘y’ represent the first and second int values to...

2 minutes read.

Awesome explanation of Strings in Java

What do you mean by String? Strings are a collection of characters that are commonly used in Java programming. Strings are regarded as objects in the Java programming language. “String” is a...

4 minutes read.

Java Map Example

In Java, the Map is an interface that is used mainly to denote key and value pairs. The central concept and theme of this mapping in the java collection framework...

4 minutes read.

Determine the Upper Bound of a Two-Dimensional Array in Java

Multi-Dimensional Array Multi-faceted exhibits in Java are regular and can be named various clusters. Information in a two-layered bunch in Java is put away in 2D even structure. A two-layered collection...

3 minutes read.