How to check whether a sequence of characters is contained in a String using the contains() method
Coding River
How to check whether a sequence of characters is contained in a String using the contains() method - Java Tutorial For Beginners
In this video, we will talk about a method that can allow us to check whether a particular sequence of characters is part of a String or not. The method we will use is called the Java String contains() method. This method takes a parameter of type String and it returns a value of type Boolean whenever it is evaluated. That means that it will return true if the specified sequence of characters is present in the String and it will return false if the sequence of characters is not. Also, the contains() method is case sensitive String sentence = "I love programming";
System.out.println(sentence.contains("love"));
System.out.println(sentence.contains("Love"));
Note that if the sequence of characters is null the program will throw an exception System.out.println(sentence.contains(null));
But an empty String will evaluate to true System.out.println(sentence.contains(""));
Since the contains() method returns a boolean value, so we can use it in an if else statement. So let’s take an example
class JavaExample{
public static void main(String args[]){
String sentence = "I love programming";
if(sentence.contains("programming")){ System.out.println(" programming is found in the string"); } else{ System.out.println("programming is nowhere to be found in the string"); } } }
Here too, since we know that contains() method is case sensitive, we can change the format of the word programming to see what will happen
if(sentence.contains("Programming")){
In the next video I will show you how we can make a case insensitive check by using a very special trick
#codingriver #java #programming ... https://www.youtube.com/watch?v=loyjMJav3iI
16341411 Bytes