Java - String endsWith() Method



The Java String endsWith() method is, used to determine whether the string ends with the specified suffix string or not. A suffix of a, string is a substring that occurs at the end of the string.

The endsWith() method returns a Boolean value, which is true if the string ends with the specified substring; false otherwise. It accepts a parameter as a string that holds the value of the specified suffix.

This method does not throw any exception while determining whether the string ends with the specified suffix or not.

Syntax

Following is the syntax of the Java String endsWith() method −

public boolean endsWith(String suffix)

Parameters

  • suffix − This is the suffix.

Return Value

This method returns true if the string ends with the specified suffix.

Example

If the given string value is the same as the specified suffix, the endsWith() method returns true.

In the following program, we are instantiating the String class with the value “Hello”. Then, using the endsWith() method, we are, trying to check whether the string ends with the specified suffix “llo” or not.

import java.lang.*;
public class CheckSuffix {
   public static void main(String[] args) {
      
      //instantiate a string class
      String str = new String("Hello");
      
      //initialize the suffix
      String suffix = "llo";
      System.out.println("The given string is: " + str);
      System.out.println("The suffix is: " + suffix);
      
      //using the endsWith() method
      System.out.println("The string ends with the specified suffix or not? " + str.endsWith(suffix));
   }
}

Output

On executing the above program, it will produce the following result −

The given string is: Hello
The suffix is: llo
The string ends with the specified suffix or not? true

Example

If the given string value is different from the specified suffix, the endsWith() method returns false.

In the following example, we are creating an object of the string class with the value “Java Programming”. Using the endsWith() method, we are checking whether the string ends with the specified suffix “Java” or not.

import java.lang.*;
public class CheckSuffix {
   public static void main(String[] args) {
      
      //create an object of the string class
      String str = new String("Java Programming");
      
      //initialize the suffix
      String suffix = "Java";
      System.out.println("The given string is: " + str);
      System.out.println("The suffix is: " + suffix);
      
      //using the endsWith() method
      System.out.println("The string ends with the specified suffix or not? " + str.endsWith(suffix));
   }
}

Output

Following is the output of the above program −

The given string is: Java Programming
The suffix is: Java
The string ends with the specified suffix or not? false

Example

If the given string value is the same as the suffix, but the case is different, the endsWith() method returns false.

In the following program, we are instantiating the string class with the value “TutoriaslPoint”. Using the endsWith() method, we are trying to check whether the string ends with the specified suffix “point” or not. Since the method is case-sensitive, it will return false.

import java.lang.*;
public class CheckSuffix {
   public static void main(String[] args) {
      
      //instantiate the string class
      String str = new String("TutorialsPoint");
      
      //initialize the suffix
      String suffix = "point";
      System.out.println("The given string is: " + str);
      System.out.println("The suffix is: " + suffix);
      
      //using the endsWith() method
      System.out.println("The string ends with the specified suffix or not? " + str.endsWith(suffix));
   }
}

Output

The above program, produces the following output −

The string is: Welcome
The suffix string is: COME
When the suffix is the same as the substring but the case are different, it will return: false

Example

Using conditional statement to check whether the string ends with the specified suffix or not.

In this program, we are creating a string literal with the value “TutorialsPoint”. Then, using the endsWith() method, we are checking whether the string ends with the specified suffix “Point” or not.

import java.lang.*;
public class CheckSuffix {
   public static void main(String[] args) {
      
      //instantiate the string class
      String str = new String("TutorialsPoint");
      
      //initialize the suffix
      String suffix = "Point";
      System.out.println("The given string is: " + str);
      System.out.println("The suffix is: " + suffix);
      
      //using the endsWith() method
      boolean bool = str.endsWith(suffix);
      if(bool) {
         System.out.println("The string ends with the specified suffix");
      } else {
         System.out.println("The string is not ends with the specified suffix");
      }
   }
}

Output

After executing the above program, it generates the following output −

The given string is: TutorialsPoint
The suffix is: Point
The string ends with the specified suffix
java_lang_string.htm
Advertisements