源码:
从源码看实现方法并不难,可知,startWith有两种入参方法:
一种是不添加索引偏移量(默认从头开始)(String,0),
另一种是添加偏移量从指定位置开始(String ,int)
以下为我的举例测试结果:
public class startwith {
public static void main(String[] args) {
String str = "abcdef";
//当字符串完全一样时,
System.out.println(str+"和“abcdef”校验结果:"+str.startsWith("abcdef"));
//结果为true
//当字符串部分一样cde,从默认0开始
System.out.println(str+"和“cde”校验结果:"+str.startsWith("cde"));
//结果为false
//当字符串部分一样cde,从指定位置2开始
System.out.println(str+"和“abcdef”校验结果:"+str.startsWith("cde",2));
//结果为true
//当字符串仅头部分一样abc,从默认位置0开始
System.out.println(str+"和“abcdef”校验结果:"+str.startsWith("abc"));
//结果为true
}
}