Products
GG网络技术分享 2025-11-14 05:27 1
java public class StringMethodsExample { public static void main { // 示例:用String类的方法
// 检查字符串是不是包含另一个字符串
String str = "hello world";
boolean result = str.contains;
System.out.println; // 输出 true
// 返回指定字符串在此字符串中Zui右边出现处的索引
int lastIndex = str.lastIndexOf;
System.out.println; // 输出 9
// 测试字符串是不是以指定的后缀收尾
boolean endsWithResult = str.endsWith;
System.out.println; // 输出 true
// 用给定的正则表达式来测试字符串是不是满足某个模式
boolean matchesResult = str.matches;
System.out.println; // 输出 true
// 测试字符串是不是以指定的前缀开头
boolean startsWithResult = str.startsWith;
System.out.println; // 输出 true
// 返回指定字符串在此字符串中第一次出现处的索引
int indexOfResult = str.indexOf;
System.out.println; // 输出 6
}
}

Demand feedback