Java.lang.String.endsWith() 方法
描述
java.lang.String.endsWith() 方法返回测试,如果此字符串以指定的 suffix 结尾。
声明
以下是 java.lang.String.endsWith() 方法的声明。
public boolean endsWith(String suffix)
参数
suffix − 这是后缀。
返回值
如果参数表示的字符序列是该对象表示的字符序列的后缀,则该方法返回true,否则返回false。
异常
NA
示例
下面的例子展示了 java.lang.String.endsWith() 方法的使用。
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);
}
}
让我们编译并运行上面的程序,这将产生下面的结果 −
www.tutorialspoint.com ends with .com ? true ends with .org ? false

