×

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

Java Math atan() Method

The atan() method of Math class computes the trigonometric Arc Tangent (inverse of tangent ) of an angle. The value returned is between -pi/2 to pi/2. Syntax: public static double atan(double a) Parameters: The...

2 minutes read.

Convert JSON File to String in Java

Before understanding the conversion of JSON file to string, one must know about JSON. What is JSON? JSON stands for JavaScript Object Notation. It is an open standard format lightweight, text-based, and...

3 minutes read.

Generic queue in Java

Before understanding how to implement a generic queue in java, one must know about generics and queue in java. Generics in Java Generics are parameterized types. The goal is to enable type...

6 minutes read.

Java Enumeration

In a computer language, enumerations express a set of named constants. For instance, the four suits in a deck of playing cards could be represented by the enumerators Club, Diamond,...

4 minutes read.

How to Create a Package in Java?

For the most part, how to create a package in Java generally, a package is defined as a collection of relevant or irrelevant items together in a very major way....

7 minutes read.

Access specifiers in Java

What are access specifiers in Java? Access specifiers in Java are used to determine whether other classes can use a particular field or invoke a particular method. Access specifiers mainly specify...

7 minutes read.

How to Convert String to float in Java

How to Convert String to Float in java It is used if you want to perform mathematical operations on the string that contains float number. You can convert String to float...

3 minutes read.

Transient variable in Java

In this article, you will be acknowledged about transient variable along with its functions. We would conclude by understanding an example program about it. Transient variable By introducing the transitory keyword, we...

3 minutes read.

How to Convert String to long in Java

How to Convert String to long in java It is used when you want to perform the mathematical operation on the String which contains long number then the conversion from String...

4 minutes read.

Java String getChars() Method

Java String getChars() method copies characters from current String to the destination character array . Syntax: public void getChars(int srcBeginIndex, int srcEndIndex, char[] destination, int dstBeginIndex) Parameters: srcBegin - index of the first character...

1 minute read.

Java 8 filters list

A stream with the components of this stream that match the given predicate is provided by the streaming filter (Predicate predicate). This process is step-by-step. Because these operations are always...

4 minutes read.

How to convert float to String in Java

How to convert float to String in java There are following methods to convert float to String: Convert using Float.toString(float) Convert using String.valueOf(float) Convert using Float.toString(float) The Float class has a static method that returns...

2 minutes read.

Hashing Algorithm in Java

The hashing algorithm is a method that maps data to the fixed-length hash. The Java hash-based algorithm employs a cryptographic mathematical operation. A hash technique or hash function is supposed...

8 minutes read.

Tetris Game in Java

The Tetris game is among the most well-known video games ever produced for computers. Today, we may engage in this game on a mobile device as well. Alexey Pajitnov conceptualized...

12 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.

How to reverse a linked list in java

The process of reversing a linked list in Java will be covered in this section. One of the most common questions in interviews is about reversing a linked list. If...

11 minutes read.

Getting the Day from the Date in Java?

To extract the day's name from the date, we will write Java application. When dealing the date and time in Java, the following classes are used. Class for Calendars: This class is...

6 minutes read.

Check if the given array is mirror inverse in Java

The primary objective is to check whether the given array is mirror inverse or not. The values and position of the specified array are switched, and a duplicate array is created....

3 minutes read.

Java Swings

Swing is a Java Foundation Class library and an extension to the Abstract Window Toolkit (AWT) (JFC).As compared to AWT, Swing has significantly better functionality, new components, increased component features,...

9 minutes read.

List all files in a Directory in Java

The list of all files in the directory can be done using Java. You should be aware that a directory may include a subfolder and that subdirectory may also contain some...

3 minutes read.