• Home
  • Testing
  • SAP
  • Web
  • Must Learn!
  • Big Data
  • Live Projects
  • AI
  • Blog

What is String "endsWith" Method?

The Java endsWith method is used to check whether the string is ending with user-specified substring or not. Based on this comparison it will return boolean True or False.

Syntax

Public endsWith(suffix) 

Parameters

suffix – This is a suffix.

Return Value

  • False: Character sequence supplied in "suffix" DOES NOT matches the end sequence of the calling string
  • True: Character sequence supplied in "suffix" matches the end sequence of the calling string

Exception

None

Example:

public class StringEx1 {
    public static void main(String[] args) {
        String str_Sample = "Java String endsWith example";
        //Check if ends with a particular sequence
        System.out.println("EndsWith character 'e': " + str_Sample.endsWith("e"));
        System.out.println("EndsWith character 'ple': " + str_Sample.endsWith("ple"));
        System.out.println("EndsWith character 'Java': " + str_Sample.endsWith("Java"));
    }
}

Output:

EndsWith character 'e': true
EndsWith character 'ple': true
EndsWith character 'Java': false

The java.lang.String.endsWith() returns tests if this string ends with the specified suffix.

 

YOU MIGHT LIKE: