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