在Java中,可以使用startsWith()
方法来检查一个字符串是否以特定的子字符串开头。示例如下:
String str = "Hello World"; if(str.startsWith("Hello")) { System.out.println("String starts with 'Hello'"); } else { System.out.println("String does not start with 'Hello'"); }
上述代码将输出:
String starts with 'Hello'
如果字符串以特定子字符串开头,则startsWith()
方法返回true
;否则返回false
。