String method contains() in Java
The String method contains(CharSequence s) used to determine whether string contains given character sequence. It returns the boolean value true if found, otherwise false. In case CharSequence is null, it throw NullPointerException.
Syntax
 1 public boolean contains(CharSequence s)
s : It is a character sequence to determine whether it is present in the source string.
return : It returns boolean value true, if character sequence present in the string, otherwise false.
Example
 1 package com.java;
 2 
 3 public class Example {
 4 
 5     public static void main(String[] args) {
 6 
 7         String str = "Good thing in Java is Java";
 8 
 9         boolean res = str.contains("Java");
 10         System.out.println("Does 'Java' present in string : " + res);
 11     }
 12 }
In the above example, a string define by assigning a string literal. A contains() method called by passing a character sequence that validate whether a specified character sequence present in the string. As specified characters Java present, it returns true. It matches an exact case of character sequence.
Output
 1 Does 'Java' present in string : true

Example 2

Example 2
 1 package com.java;
 2 
 3 public class Example {
 4 
 5     public static void main(String[] args) {
 6 
 7         String str = "Good thing in Java is Java";
 8 
 9         boolean res = str.contains("JaVa"); // different case
 10         System.out.println("Does 'Java' present in string : " + res);
 11     }
 12 }
In the above example, a contains() method called by passing a string with different case, it compares string with exact case and returns boolean value false. As it does not match the specified case in source string.
Output
 1 Does 'Java' present in string : false

String method contains() with collection

String method contains() with collection
 1 package com.java;
 2 
 3 import java.util.Arrays;
 4 import java.util.List;
 5 
 6 public class Example {
 7 
 8     public static void main(String[] args) {
 9 
 10         List<String> list = Arrays.asList("Starting", "with", "string");
 11 
 12         String str = "ing";
 13         list.forEach(ele -> {
 14             System.out.println("Does string includes specified string :" + ele.contains(str));
 15         });
 16     }
 17 }
In the above example, a list of string define and iterated using collection method forEach() method by specifying a lambda expression. It validates whether an element includes specifying characters sequence. It returns boolean value that concatenated with string and print.
Output
 1 Does string includes specified string :true
 2 Does string includes specified string :false
 3 Does string includes specified string :true

String method contains() with Stream API

String method contains() with Stream API
 1 package com.java;
 2 
 3 import java.util.Arrays;
 4 import java.util.List;
 5 
 6 public class StreamExp {
 7 
 8     public static void main(String[] args) {
 9 
 10         List<String> list = Arrays.asList("Starting", "with", "string");
 11 
 12         String str = "ing";
 13         list.stream().filter(ele -> ele.contains(str))
 14                 .forEach(System.out::println);
 15     }
 16 }
In the above example, a list of string define and collection method stream() called that returns Stream object. A Stream object filter() called by specifying a lambda expression that validates whether an element includes specified character sequence and returns boolean value. A filter() method returns Stream object that includes elements that returns boolean value true, a Stream method forEach() method called that prints resulting elements.
Output
 1 Starting
 2 string
Privacy Policy
Terms of Service
Disclaimer
Contact us
About us