String類的endsWith()方法和startsWith()方法


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

 

public class Test {
    public static void main(String args[]) {
        String Str = new String("xiaohouzi.com");
        boolean real;

        real= Str.endsWith( "cn" );
        System.out.println( real);

        real= Str.endsWith( "com" );
        System.out.println( real);
    }
}

 

執行結果為:false

      true

 

 

 

String類的startsWith() 方法用於檢測字符串是否以指定的前綴開始,如果字符串以指定的前綴開始,則返回 true;否則返回 false。

語法:public boolean startsWith(String prefix, int toffset)   或   public boolean startsWith(String prefix)

參數:prefix -- 前綴 

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

 

 

public class Test {
    public static void main(String args[]) {
        String Str = new String("xiaohouzi.com");

        System.out.println(Str.startsWith("xiao") );

        System.out.println(Str.startsWith("ao") );

        System.out.println(Str.startsWith("hou", 4) );
    }
}

執行結果為:true

      false

      true

 


免責聲明!

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



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