startsWith()方法检查字符串是否以指定的字符开头。
提示:使用endsWith()方法判断字符串是否以指定的字符结尾。
public boolean startsWith(String chars)
| 参数 | 描述 |
| chars | String,代表要判断的字符 |
| 返回值: | boolean值,如果字符串以指定字符开头,则返回true,如果字符串不是以指定的字符开头,则返回false。 |
例如:
找出字符串是否以指定字符开头:
String myStr = "Hello";
System.out.println(myStr.startsWith("Hel")); // true
System.out.println(myStr.startsWith("llo")); // false
System.out.println(myStr.startsWith("o")); // false