Java String contains() method checks whether a particular string is a part of another string or not. This method returns true if a specified sequence of characters is present in a given string, else it returns false.
For example:
String str = "Game of Thrones";  

//This will print "true" because "Game" is present in the given String
System.out.println(str.contains("Game"));

/* This will print "false" because "aGme" is not present, the characters
 * must be present in the same sequence as specified in the contains method
 */
System.out.println(str.contains("aGme"));

Syntax of contains() method

public boolean contains(CharSequence str)
The return type is boolean which means this method returns true or false. When the character sequence found in the given string then this method returns true else it returns false.
If the CharSequence is null then this method throws NullPointerException.
For example: calling this method like this would throw NullPointerException.
str.contains(null);

Java String contains() method Example

The second print statement displayed false because the contains() method is case sensitive. You can also use the contains() method for case insensitive check, I have covered this at the end of this tutorial.
class Example{  
   public static void main(String args[]){  
 String str = "Do you like watching Game of Thrones";  
 System.out.println(str.contains("like"));
  
 /* this will print false as the contains() method is
  * case sensitive. Here we have mentioned letter "l" 
  * in upper case and in the actual string we have this
  * letter in the lower case. 
  */
 System.out.println(str.contains("Like")); 
 System.out.println(str.contains("Game")); 
 System.out.println(str.contains("Game of")); 
   }
}
Output:
true
false
true
true

Example 2: Using Java String contains() method in the if-else statement

class Example{  
   public static void main(String args[]){  
 String str = "This is an example of contains()";  
 /* Using the contains() method in the if-else statements, since
  * this method returns the boolean value, it can be used
  * in the if else conditions whenever needed.
  */
 if(str.contains("example")){
    System.out.println("The word example is found in given string");
 }
 else{
    System.out.println("The word example is not found in the string");
 }
   }
}
Output:
The word example is found in given string

Java String contains() method for case insensitive check

We have seen above that the contains() method is case sensitive, however with a little trick, you can use this method for case insensitive checks. Lets take an example to understand this:
Here we are using the toLowerCase() method to convert both the strings to lowercase so that we can perform case insensitive check using contains() method. We can also use toUpperCase() method for this same purpose as shown in the example below.
class Example{  
  public static void main(String args[]){  
 String str = "Just a Simple STRING"; 
 String str2 = "string";
 //Converting both the strings to lower case for case insensitive checking
 System.out.println(str.toLowerCase().contains(str2.toLowerCase()));
  
 //You can also use the upper case method for the same purpose.
 System.out.println(str.toUpperCase().contains(str2.toUpperCase()));
  }
}
Output:
true
true

Reference:

1 comments :

  1. Thank you for this information, I have got something to acknowledge. Please have a read.

    Making of any website or software to customize it and develop a quality product, InnovationM is known for its quality assured handcrafted technological solution, going from software development to website design and Mobile application development. InnovationM is the best Mobile app development and Website design Company in London, UK. Thus developing a core competition section of uniformly design and developed product, that's where we excel at.

    We are a Software development and web design company providing custom website solutions, law firm web design services in derby, Luton, Birmingham, Sheffield, Derby, and London, UK.

    And also popularly known for website development in cambridge birmingham, software design agency derby, mobile app design agency derby, app development luton, android app design agency derby, app developers luton, app design bristol, software design company derby, ios app development company derby, iphone app development company derby, mobile app design derby, bespoke app development derby, android app development company in derby, software app development derby, software development company brighton, app agency oxford, mobile app development company derby, ios app development derby, iphone app development derby,UK.
    For more: www.innovationm.co.uk

    ReplyDelete

 
Top