public class Main {
public static void main(String[] args) {
// 定义一个字符串
String str = "Hello, world!";
// 使用 startsWith 方法判断字符串是否以指定前缀开头
boolean result1 = str.startsWith("Hello"); // 返回 true
boolean result2 = str.startsWith("world"); // 返回 false
// 打印结果
System.out.println("字符串以 'Hello' 开头: " + result1);
System.out.println("字符串以 'world' 开头: " + result2);
// 也可以使用 startsWith 方法并指定起始位置
boolean result3 = str.startsWith("world", 7); // 返回 true
System.out.println("从索引 7 开始,字符串以 'world' 开头: " + result3);
}
}
startsWith
方法用于检查字符串是否以指定的前缀开头。true
,否则返回 false
。startsWith
方法还可以接受第二个参数,表示从字符串的哪个索引位置开始检查前缀。上一篇:java 编程
下一篇:如何运行java文件
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站