Table of Contents
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.
When you run above program, you will get below output:
Method Signature:
1 2 3 |
public boolean contains(CharSequence s) |
String contains Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
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"); } } } |
1 2 3 4 |
String contains with world String does not contain WORLD |
Please note that contains method is case sensitive as you can see in above example
Was this post helpful?
Let us know if this post was helpful. Feedbacks are monitored on daily basis. Please do provide feedback as that\'s the only way to improve.