×

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

Difference between JIT and JVM in Java

In this tutorial, we will discuss the difference between JIT (Just In Time Compiler) and JVM (Java Virtual Machine) in Java. Before we move to the differences, let's understand what...

4 minutes read.

Java Enum vs Class

Enumerations are used in programming languages to represent collections of named constants. For instance, the four suits in a deck of playing cards might represent four integrators named Club, Diamond,...

7 minutes read.

Java Boolean Class

The Boolean class wraps a value of the Boolean primitive in an object. Its object contains only a single field whose type is Boolean. Boolean Methods: This class contains several different methods...

2 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 8 Features

Java 8 Features: After the great success of Java 7, many developers were waiting for the next version of one of the best languages in the tech world. Moreover, on...

6 minutes read.

Practical Number in Java

In this tutorial, we will understand what is meant by practical numbers. We will understand it throughthe aid of examples and implementation in a java programming language. The practical numbers...

5 minutes read.

Java Boolean equals() method

The equals() method of Java Boolean class returns a Boolean value true if the specified argument is not null and is same as this object, else it returns false. Syntax public boolean...

2 minutes read.

Why to use Enum in Java?

In this article, you will be acknowledged about the Enum in java, its uses and mainly its purpose. Enum has various functionalities. Each of them will be discussed. Enum In a computer...

3 minutes read.

Comparator vs Comparable in Java

Comparator Vs Comparable in Java In Java, sorting of primitive data types can be done using different inbuilt functions that are available. But for sorting the collection of objects or types...

4 minutes read.

Arrow Operator in Java

The introduction of arrow functions in ES6 gives you a more precise approach to define JavaScript functions. We can write shorter function syntax thanks to them. Your code will be...

4 minutes read.

Advanced Java skills

These were some of the contemporary talents that a Java developer should master in efforts to progress in the era of science in 2022. A database such as MySQL, a...

4 minutes read.

Star Program in Java

By solving the patterns, we can develop our coding skills and logical thinking. Mostly each pattern program uses two or more loops. Loops' number depends on the complexity of logic....

3 minutes read.

JAR File in Java

What is a JAR File? JAR stands for Java Archive. It is a file format mainly used to combine many Java class files and the corresponding metadata and resources into one...

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.

String Concatenation in Java

In Java, it gathers a new String that combines several strings. Following are the manners to concatenate strings in Java: By + (String concatenation) operatorBy concat() method By + (String concatenation) operator Java...

4 minutes read.

Java Math sqrt() Method

The sqrt() method of Java Math class returns the accurately rounded positive square root of the specified double value. Syntax: public static double sqrt(double a) Parameters: The parameter ‘a’ represents the number whose square...

2 minutes read.

Static class in Java

In Java, a class is a file containing the Java byte code. It can essentially specifically be executed on the JVM (Java Virtual Machine), fairly significant. The JVM is mostly...

7 minutes read.

Java Long Keyword

Long is a primitive data type in Java. To initilize variables, we implement the long keyword. It can also be applied to techniques. A 64-bit two's complement integer can put...

3 minutes read.

Alien language problem in Java

Given the alphabetic sequence of an alien language, given a sorted dictionary (array of words) for the languages. Example: Words = { "aac", "abc", "aaa" } Output c, a, b Algorithm: (1) Compare two words that...

3 minutes read.

Big Decimal class in Java

The fairly Big pretty Decimal class provides operations for arithmetic, rounding, comparison and format conversion in a sort of big way. It can handle generally large and very small floating-point...

6 minutes read.