Java String startsWith() 方法用于检查字符串是否以某个子字符串开头。
startsWith() 方法是重载方法,有两种形式:
- boolean startsWith(String str) - 如果
str
是字符串的前缀,则返回true
。 - boolean startsWith(String str, int fromIndex) - String 从指定索引
fromIndex
开始, 如果以str
开头,则返回true
。
更多: zhilu jiaocheng
Java String startsWith(String) 示例
public class StringExample { public static void main(String[] args) { String blogName = "hello world hello java welcome to onitroad world"; // 字符串是否以 hello开头 System.out.println( blogName.startsWith("hello") ); System.out.println( "onitroad.com".startsWith("on") ); System.out.println( "onitroad.com".startsWith("hello") ); // 使用第2种重载方法 // 字符串从第18个索引开始,是否以java开头 System.out.println( blogName.startsWith("java", 18) ); } }
日期:2020-09-17 00:10:11 来源:oir作者:oir