Java String類中的startsWith()、endsWith()


Java String類中的startsWith()、endsWith()

1、startsWith() 方法用於檢測字符串是否以指定的前綴開始。

語法

public boolean startsWith(String prefix, int toffset)

或

public boolean startsWith(String prefix)

參數

  • prefix -- 前綴。

  • toffset -- 字符串中開始查找的位置。

返回值

如果字符串以指定的前綴開始,則返回 true;否則返回 false。

實例

public class Test {
    public static void main(String args[]) {
        String str = new String("www.runoob.com");
 
        System.out.print("返回值 :" );
        System.out.println(str.startsWith("www") );
 
        System.out.print("返回值 :" );
        System.out.println(str.startsWith("runoob") );
 
        System.out.print("返回值 :" );
        System.out.println(str.startsWith("runoob", 4) );
    }
}

以上程序執行結果為:

  返回值 = true

  返回值 = false

  返回值 = true

2、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.runoob.com");

        System.out.println("返回值 = " + str.endsWith( "runoob" ));

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

以上程序執行結果為:

  返回值 = false

  返回值 = true


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM