Java String contains() method is used to check if the string contains specified character sequence or not.
Java String contains()
- Java String contains() method was introduced in java 1.5 release as a utility method.
- This method returns
true
if the string contains the specified sequence of char values. Else it returns false. - String contains() method internally uses String
indexOf
method, below code snippet shows the contains() method definition.
12345public boolean contains(CharSequence s) {return indexOf(s.toString()) >= 0;} - If the argument is null, then NullPointerException is thrown, it’s clear from the above code that there is no null check for the argument string. For example;
"abc".contains(null);
will throw below exception stack trace similar to below.
12345Exception in thread "main" java.lang.NullPointerExceptionat java.base/java.lang.String.contains(String.java:2047)at com.journaldev.string.JavaStringContains.main(JavaStringContains.java:17) - Note that string contains() method is case sensitive, so
"abc".contains("A");
will returnfalse
. - The argument of String contains() method is
java.lang.CharSequence
, so any implementation classes are also fine as argument, such as StringBuffer, StringBuilder andCharBuffer
.
Java String contains() method simple example
Let’s look at a simple java string contains method example.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
package com.journaldev.string; /** * Java String contains example * @author pankaj * */ public class JavaStringContains { public static void main(String[] args) { String str = "abc def"; String str1 = "//ab/\"; System.out.println(str.contains("a")); //true System.out.println(str.contains("\")); //false System.out.println(str1.contains("\")); //true System.out.println(str1.contains("/")); //true boolean flag = "abc".contains("A"); //false } } |
The output of the above program statements are shown in the comments itself and self understood.
Java String contains() with case insensitive check
For case insensitive check, we can change both the strings to either upper case or lower case before calling the contents() method.
1 2 3 4 |
"abc".toLowerCase().contains("A".toLowerCase()); //true "abc".toUpperCase().contains("A".toUpperCase()); //true |
Java String contains() with CharSequence
Let’s look at examples of using String contains() method with StringBuffer
, StringBuilder
and CharBuffer
. All these are implementations of CharSequence
interface. Also note that CharBuffer
is an abstract class, creating it’s instance is slightly different from other usual classes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
//Java String contains with StringBuffer StringBuffer sb = new StringBuffer("User"); System.out.println(str.contains(sb)); //true //Java String contains with StringBuilder StringBuilder sb1 = new StringBuilder(sb); System.out.println(str.contains(sb1)); //true //Java String contains with CharBuffer CharBuffer cb = CharBuffer.allocate(4); cb.append('U');cb.append('s');cb.append('e');cb.append('r'); cb.clear(); System.out.println(cb); //User System.out.println(str.contains(cb)); //true |
That’s all for java string contains method examples. If you are learning java, I would strongly recommend to go through core java tutorial.
Reference: API Doc