String类的startsWith(String prefix,int toffset)方法测试此字符串的子字符串是否从指定索引处开始。
示例
import java.lang.*;
public class StringDemo {
public static void main(String[] args) {
String str = "www.nhooo.com";
System.out.println(str);
//要检查的起始字符串
String startstr1 = "nhooo";
String startstr2 = "nhooo";
//检查字符串是否以给定的子字符串和起始索引开头
boolean retval1 = str.startsWith(startstr1);
boolean retval2 = str.startsWith(startstr2, 4);
//如果字符串以给定的子字符串开头,则输出true-
System.out.println("starts with " + startstr1 + " ? " + retval1);
System.out.println("string " + startstr2 + " starting from index 4 ? " + retval2);
}
}
输出结果www.nhooo.com
starts with nhooo ? false
string nhooo starting from index 4 ? true