Java startsWith() 方法
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.xuhuhu.com");
System.out.print("返回值 :" );
System.out.println(Str.startsWith("www") );
System.out.print("返回值 :" );
System.out.println(Str.startsWith("zaixian") );
System.out.print("返回值 :" );
System.out.println(Str.startsWith("zaixian", 4) );
}
}
以上程式執行結果為:
返回值 :true 返回值 :false 返回值 :true