Java.lang.String.endsWith() Method

Advertisements


Description

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

Declaration

Following is the declaration for java.lang.String.endsWith() method

public boolean endsWith(String suffix)

Parameters

  • suffix -- This is the suffix.

Return Value

This method returns a true if the character sequence represented by the argument is a suffix of the character sequence represented by this object, else false.

Exception

  • NA

Example

The following example shows the usage of java.lang.String.endsWith() method.

package com.tutorialspoint;

import java.lang.*;

public class StringDemo {

  public static void main(String[] args) {
  
    String str = "www.tutorialspoint.com";
    System.out.println(str);

    // the end string to be checked
    String endstr1 = ".com";
    String endstr2 = ".org";

    // checks that string str ends with given substring
    boolean retval1 = str.endsWith(endstr1);
    boolean retval2 = str.endsWith(endstr2);

    // prints true if the string ends with given substring
    System.out.println("ends with " + endstr1 + " ? " + retval1);
    System.out.println("ends with " + endstr2 + " ? " + retval2);
  }
}

Let us compile and run the above program, this will produce the following result:

www.tutorialspoint.com
ends with .com ? true
ends with .org ? false



Advertisements
Advertisements