Pages

Monday, March 4, 2019

String Contains() method in java with example - Internal Implementation

String contains() method in java with example:


The contains() method is Java method to check if String contains another substring or not. It returns boolean value so it can use directly inside if statements. This method returns true only if this string contains "s" else false. NullPointerException − if the value of s is null. This method is introduced in java 1.5 version.



String Contains() method


Syntax:

public boolean contains​(CharSequence s)

Parameters:

s - the sequence to search for


Return Value:

This method returns true only if and only this string contains "s" else false. 

Exception:

NullPointerException − if the s value of s is null.




String contains() Example 1:

Example program to check the given string in present in the input string or not.

package examples.java.w3schools.string;

public class StringContainsExample {
 public static void main(String[] args) {

  String value1 = "Welcome to java-w3schools blog";
  String value2 = "w3schools";
  String value3 = "java";
  String value4 = "venkatesh";

  System.out.println("value1 : "+value1);
  System.out.println("value2 : "+value2);
  System.out.println("value3 : "+value3);

  // We will search now value2 is present in value1.
  System.out.println("Value1 contains value2 : " + value1.contains(value2));
  
  // We will search now value3 is present in value1.
  System.out.println("Value1 contains value3 : " + value1.contains(value3));
  
  // We will search now value4 is present in value1.
  System.out.println("Value1 contains value4 : " + value1.contains(value4));
  
 }
}


Output:

value1 : Welcome to java-w3schools blog
value2 : w3schools
value3 : java
Value1 contains value2 : true
Value1 contains value3 : true
Value1 contains value4 : false


Here input string is "Welcome to java-w3schools blog" and checking "w3schools", "java" and "venkatesh" are present in input string. If it contains, returns ture. Otherwise, returns false.
First two contains statements print true. Last contains statement prints false because it is not in input string.

String contains() Example 2:

When to use contains() method? This method is really very helpful in the projects to check the specified string present in a particular strig. For example, we want to find the string if "Michael Jackson Became The King Of Pop" has "King" in it. This method is useful in such situation.

We will test with "King" and lowercase "king".


package examples.java.w3schools.string;

public class StringContainsExample2 {
 public static void main(String[] args) {

  String input = "Michael Jackson Became The King Of Pop";

  if (input.contains("King")) {
   System.out.println("Yes, Michael Jackson is The King Of Pop");
  } else {
   System.out.println("Noone will say Michael Jackson is not The King Of Pop");
  }
 }
}

String Contains() method-2

Output:

Yes, Michael Jackson is The King Of Pop


This example is based on if else condition.

String contains() NullPointerException example:

If we pass null to the contains method then will throw run-time exception saying NullPointerException.



package examples.java.w3schools.string;

public class StringContainsNullPointerEx {
 public static void main(String[] args) {

  String input = "Stars are visible during night.";

  System.out.println(input.contains(null));
 }
}

Output:

Exception in thread "main" java.lang.NullPointerException
 at java.base/java.lang.String.contains(String.java:2036)
 at w3schools/examples.java.w3schools.string.StringContainsNullPointerEx.main(StringContainsNullPointerEx.java:8)


String Contains() method Internal Implementation:

We will see now how Contains method is implemented internally or how Contains works internally.

Following is the contains method code in String class.

   public boolean contains(CharSequence s) {
        return indexOf(s.toString()) >= 0;
    }

contains method internally calls indexof method.

No comments:

Post a Comment

Please do not add any spam links in the comments section.