1、定义和用法

startsWith()方法检查字符串是否以指定的字符开头。

提示:使用endsWith()方法判断字符串是否以指定的字符结尾。

2、调用语法

public boolean startsWith(String chars)

3、参数说明

参数 描述
chars String,代表要判断的字符

4、方法说明

返回值: 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