This function is specially designed to check if a string ‘contains’ another string or not. If you’re new to this you might end up using it to find a ‘character’. But it won’t serve the purpose.
In this article, we’ll address how java.lang.String.contains() is used, implemented and what exceptions can arise if not used carefully.![Java String contains() Method - 1]()
![Java String contains() Method - 2]()
What is the contains() method?
You can use the contains(String key) method to “find” if a certain string “key” is present within a certain string or not. If “key” is found, “true” is returned. Else you will get a “false”.
Internal Implementation
This method is already implemented by java.lang.String. You don’t have to implement this yourself. Here’s a quick explanation of it for your understanding.
public class ContainsMethod
{
public boolean contains(CharSequence key)
{
return indexOf(key.toString()) > -1;
}
}
Code Explanation
contains() method, takes a CharSequence as an input parameter. Which is later on converted to a “String”. Then this expression is computed indexOf(key.toString()) > -1;. Which means, if that “key” is found at any index ( “0” or greater ) then “true” is returned. And if the key is not found, then a “false” is returned.How to use the contains() method?
Here’s how you can use it.
public class ContainsMethod {
public static void main(String[] args) {
String input = "A brown fox jumped over a lazy dog.";
// check the containing strings
System.out.println("input.contains(bro) = " + input.contains("bro"));
System.out.println("input.contains(brown) = " + input.contains("brown"));
System.out.println("input.contains(Brown) = " + input.contains("Brown"));
System.out.println("input.contains(fox) = " + input.contains("fox"));
System.out.println("input.contains(xof) = " + input.contains("xof"));
System.out.println("input.contains(dog) = " + input.contains("dog"));
System.out.println("input.contains(lazyy) = " + input.contains("lazyy"));
System.out.println("input.contains(jumping) = " + input.contains("jumping"));
}
}
Output
input.contains(bro) = true
input.contains(brown) = true
input.contains(Brown) = false // false because case-sensitive
input.contains(fox) = true
input.contains(xof) = false // false because order should be the same
input.contains(dog) = true
input.contains(lazyy) = false // false because whole substring not found
input.contains(jumping) = false
Code Explanation
Please note, this method is case-sensitive for input parameters. So in the above snippet, you can observe when you search “brown” true is returned, whereas false is returned for “Brown”. Also, you’ll get true if you find “fox” and false for “xof” or “oxf” because the order of characters needs to be the same. Lastly, if you find “jump” or “jumped” you will get a true as the whole parameter is present in the “input” string. Whereas, if you check for “jumping” false is returned because the entire key (“jumping”) is not found.Taking care of Exceptions
java.lang.String.contains() method results in a Null Pointer Exception if you forget to initialize the parameter string with some concrete value.
public class ContainsMethod {
public static void main(String[] args) {
String input = "Here is a test string.";
String test = null;
// check what happens if you look for a null string
System.out.println("input.contains(test) = " + input.contains(test));
}
}
Output
Exception in thread "main" java.lang.NullPointerException
at java.lang.String.contains(String.java:2133)
at ContainsMethod.main(ContainsMethod.java:8)
GO TO FULL VERSION