Java endsWith() Method

Java String类Java String Class


The endsWith() method is used to test whether a string ends with the specified suffix.

Syntax

public boolean endsWith(String suffix)

Parameters

  • suffix-- the specified suffix.

Return Value

Returns true if the character sequence represented by the argument is a suffix of the character sequence represented by this object; otherwise, returns false. Note that if the argument is an empty string, or is equal to this String object (as determined by the equals(Object) method), the result is true.

Example

public class Test { public static void main(String args[]) { String Str = new String("Example: www.example.com"); boolean retVal; retVal = Str.endsWith( "example" ); System.out.println("Return value =" + retVal ); retVal = Str.endsWith( "com" ); System.out.println("Return value =" + retVal ); } }

The output of the above program is:

返回值 = false
返回值 = true

Java String类Java String Class

Other Extensions