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";

  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

Written by Arpit:

If you have read the post and liked it. Please connect with me on Facebook | Twitter | Google Plus


Java tutorial for beginners Copyright © 2012