Java String contains example

String’s contains method checks if sequence of characters can be found in String. It returns true if character sequence is present in String else return false.

Method Signature:

public boolean contains(CharSequence s)

String contains Example:

package org.arpit.java2blog;

public class StringContainsMethodExample {

 public static void main(String[] args) {
  String str = "Hello world from java2blog.com";

  if (str.contains("world")) {
   System.out.println("String contains with world");
  } 
  else {
   System.out.println("String does not contain world");
  }

  if (str.contains("WORLD")) {
   System.out.println("String contains WORLD");
  } 
  else {
   System.out.println("String does not contain WORLD");
  }
 }
}

When you run above program, you will get below output:

String contains with world
String does not contain WORLD

Please note that contains method is case sensitive as you can see in above example

[social_share share_on="facebook,twitter,linkedin,whatsapp"][post_navigator in_same_term="true"]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.