Latest web development tutorials

Java endsWith() 方法

Java String類 Java String類


endsWith() 方法用於測試字符串是否以指定的後綴結束。

語法

public boolean endsWith(String suffix)

參數

  • suffix --指定的後綴。

返回值

如果參數表示的字符序列是此對象表示的字符序列的後綴,則返回true;否則返回false。 注意,如果參數是空字符串,或者等於此String 對象(用equals(Object) 方法確定),則結果為true。

實例

public class Test {
	public static void main(String args[]) {
		String Str = new String("本教程:www.w3big.com");
		boolean retVal;

		retVal = Str.endsWith( "w3big" );
		System.out.println("返回值 = " + retVal );

		retVal = Str.endsWith( "com" );
		System.out.println("返回值 = " + retVal );
	}
}

以上程序執行結果為:

返回值 = false
返回值 = true

Java String類 Java String類