This Tutorial Explains What is Java String contains() Method, its Usage, Syntax, and Various Scenarios with the help of Examples:
This tutorial will help you to understand how to check a Java substring with respect to the main String with the help of contains() Java method. Upon going through this tutorial, you will definitely be able to understand and write the Java String programs that require .contains() method for various String operations.
Apart from these, we will also take a look at some programming examples along with the FAQs for a better understanding of the topic.
=> Take A Look At The Java Beginners Guide Here.
Table of Contents:
Java String contains() Method
As discussed in the previous tutorial (Java String – Overview of methods), this method is used to check whether a substring is a part of the main String. The return type is Boolean.
Syntax of Java String contains() method is given as:
boolean contains(CharSequence str)
This will return true if the invoking Object contains the String specified by the String variable str. Otherwise, if it does not contains the String, it will return false.
For Example, We have a String variable str initialized with the value “Grand Theft Auto 5”. We need to check whether “Theft” (which is a substring) is a part of str or not.
Then we can use the String contains() Java method as:
str.contains(“Theft”);
Upon printing the above line of code, we will get the result as “true”.
package codes; public class Contains { public static void main(String[] args) { String str = "Grand Theft Auto 5"; System.out.println(str.contains("Theft")); } }
Output:
Again, if we want to check whether “Thetf” is a part of the same str variable, then we can use the same line of code by replacing with the new value to the substring which can be given as:
str.contains(“Thetf”);
This will give the result as “false”.
package codes; public class Contains { public static void main(String[] args) { String str = "Grand Theft Auto 5"; System.out.println(str.contains("Thetf")); } }
Output:
Programming Example
Here is an example of the .contains() Java method.
In this example, we will initialize a String with the value as:
String str = "Article on Java String contains";
Now, we will check different substrings as whether they are a part of the main String str or not.
package codes; public class Contains { public static void main(String[] args) { String str = "Article on Java String contains"; System.out.println(str.contains("Java")); //Java is a part of the main String str, so it will return true System.out.println(str.contains("java")); //java is not a part of the main String as it is case sensitive System.out.println(str.contains("vaJa")); //vaJa is not a part of main String due to character sequence, so it will return false System.out.println(str.contains(" ")); //Space is a part of the main String, so it will return true } }
Output:
Explanation of Example:
In the above example, you can see the first print statement that returns true as “Java” is a part of the main String str. The second and third print statement returns false due to the character case and sequence mismatch. The last print statement returns true as ” ” or whitespace is a part of the main String.
Various Scenarios
Let’s understand the .contains() method in detail. Here we will try to analyze different scenarios and the output of each case.
Scenario1: Consider the following two Strings.
String str1 = “JAVA STRING CONTAINS”;
String str2 = “string”;
Now compare the substring str2 with the main String str1 in such a way that the output should be true.
Answer: Below is the program where we have first converted the str2 into uppercase and then checked with the main String str1 with the help of the Java contains() method. You can also convert the main String str1 into lowercase and then check with str2. Either way, it will work.
package codes; public class Contains { public static void main(String[] args) { String str1 = "JAVA STRING CONTAINS"; String str2 = "string"; String str3 = str2.toUpperCase(); //This will convert the str2 into uppercase System.out.println(str1.contains(str3)); } }
Output:
Scenario2: Consider any String of your choice and incorporate an if-else statement using the Java String contains() method.
Answer: Here we have initialized the main String str1 and a substring str2. Then we have checked for the if condition as to whether str1 (String) contains str2 (substring) or not. If it contains, then print “Returns True” else print “Returns False”.
package codes; public class Contains { public static void main(String[] args) { String str1 = "The Topic is: Java String contains"; String str2 = "Java"; if(str1.contains(str2)) { System.out.println("Returns True"); } else { System.out.println("Returns False"); } } }
Output:
Frequently Asked Questions
Q #1) What happens when we pass a null value in the substring?
Answer: If we pass a null value in the substring, then it will throw “NullPointerException”.
package codes; public class Contains { public static void main(String[] args) { String str1 = "This is an exception"; System.out.println(str1.contains(null)); } }
Output:
Q #2) Can we use Java .contains() with StringBuffer?
Answer: Yes.
Given below is the example of how to use Java String .contains() with StringBuffer.
package codes; public class Contains { public static void main(String[] args) { String str1 = "Java is a programming language"; StringBuffer stb = new StringBuffer("language"); System.out.println(str1.contains(stb)); } }
Output:
Q #3) Is contains() method case sensitive in Java?
Answer: Yes, Java contains() method is case sensitive. To overcome this, you can convert the substring into lowercase or uppercase and then use the contains() method.
Q #4) What is a substring of a String?
Answer: A substring is a part of the String which occurs in the same character sequence. For example, “Help” is a substring of the “Softwaretestinghelp”.
Q #5) How do you ignore a case in Java?
Answer: In Java, we can either change the character case using toLowerCase() or toUpperCase() method. Moreover, there are multiple methods that allow you to ignore the case of a character. For Example, .equalsIgnoreCase(), .compareToIgnoreCase() and so on.
Q #6) Is null a keyword in Java?
Answer: In Java, null is literal. It is case sensitive as well. So we can not write null as NULL or Null.
Q #7) Can a String be null in Java?
Answer: Yes, a String can be null in Java.
There is a difference in the below two statements.
String str1 = ""; String str2 = null;
The first line is an empty String of length = 0.
The second line is a string variable with the null value or no value. There is no String instance in this case.
Conclusion
In this tutorial, we have understood the Java String .contains() method in detail. Now we are in a position to check whether a substring is a part of the main String using the Java .contains() method.
Moreover, each scenario given in this tutorial is unique and will help you in finding solutions to many String related problems. Lastly, the programming examples along with the FAQs that are given here will also help you in understanding the String contains() Java method in detail.
=> Read Through The Easy Java Training Series.