Which of these methods is used to know whether a string contains true

P. S. - Let me know if you come across any other way to check if a string contains a substring or not. Also, if you are learning Java from scratch then I suggest sticking with a good book like Java: How to Program by Dietel to learn fundamentals before jumping around.

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.

What You Will Learn:

    • (new Image()).src = 'https://capi.connatix.com/tr/si?token=d493855a-0d62-46c8-8552-26549ded5489&cid=f8a5d881-907e-47b5-a4f5-6980d40520cc'; cnx.cmd.push(function() { cnx({ playerId: "d493855a-0d62-46c8-8552-26549ded5489" }).render("f1b665d743fc45cbac4851003ee8cd11"); });
  • Java String contains() Method
    • Programming Example
    • Various Scenarios
    • Frequently Asked Questions
  • Conclusion
    • Recommended Reading

Which of these methods is used to know whether a string contains true

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:

Which of these methods is used to know whether a string contains true

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:

Which of these methods is used to know whether a string contains true

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:

Which of these methods is used to know whether a string contains true

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:

Which of these methods is used to know whether a string contains true

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:

Which of these methods is used to know whether a string contains true

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:

Which of these methods is used to know whether a string contains true

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.

str.contains(“Theft”);
0

Output:

Which of these methods is used to know whether a string contains true

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.

str.contains(“Theft”);
1

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.

Which methods of class String is used to check whether a given?

Which of these methods of class String is used to check whether a given object starts with a particular string literal? Explanation: Method startsWith() of string class is used to check whether the String in question starts with a specified string. It is a specialized form of method regionMatches().

How to use boolean contains in Java?

The Java String contains() method is used to check whether the specific set of characters are part of the given string or not. It returns a boolean value true if the specified characters are substring of a given string and returns false otherwise. It can be directly used inside the if statement.

Which of the following about String class is true?

Which is a true statement for object of String class? Explanation: The object of string class are mostly immutable. This means that the String objects are constant. These can't be changed once created.

Which of these is a boolean test method used in String operations?

parseBoolean(String s) − This method accepts a String variable and returns boolean. If the given string value is "true" (irrespective of its case) this method returns true else, if it is null or, false or, any other value it returns false.