Java.lang.String.contains() Method

Advertisements


Description

The java.lang.String.contains() method returns true if and only if this string contains the specified sequence of char values.

Declaration

Following is the declaration for java.lang.String.contains() method

public boolean contains(CharSequence s)

Parameters

  • s -- This is the sequence to search for.

Return Value

This method returns true if this string contains s, else false.

Exception

  • NullPointerException -- if s is null.

Example

The following example shows the usage of java.lang.String.contains() method.

package com.tutorialspoint;

import java.lang.*;

public class StringDemo {

  public static void main(String[] args) {
  
    String str1 = "tutorials point", str2 = "http://";

    CharSequence cs1 = "int";
    
    // string contains the specified sequence of char values
    boolean retval = str1.contains(cs1);
    System.out.println("Method returns : " + retval);
    
    // string does not contain the specified sequence of char value
    retval = str2.contains("_");   
    System.out.println("Methods returns: " + retval);
  }
}

Let us compile and run the above program, this will produce the following result:

Method returns : true
Methods returns: false



Advertisements
Advertisements