在Java中字符串查找的形式有以下几种:
例如:
String str = "Hello, world!";
int index = str.indexOf("world");
System.out.println(index); // 输出6
例如:
String str = "Hello, world!";
int index = str.lastIndexOf("o");
System.out.println(index); // 输出8
例如:
String str = "Hello, world!";
boolean contains = str.contains("world");
System.out.println(contains); // 输出true
例如:
String str = "Hello, world!";
boolean isMatch = str.matches("Hello.*");
System.out.println(isMatch); // 输出true
例如:
String str = "Hello, world!";
boolean startsWith = str.startsWith("Hello");
boolean endsWith = str.endsWith("world!");
System.out.println(startsWith); // 输出true
System.out.println(endsWith); // 输出true
这些方法可以根据具体的需求选择合适的方式进行字符串查找。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读:java字符串查找的方法是什么