Java 程序,在字符串中查找子字符串
要查找字符串中的子字符串,请使用 indexOf() 方法。
我们假设以下为我们的 字符串。
String str = "testdemo";
在字符串中查找子字符串“demo”并获取索引。
int index = str.indexOf( 'demo');
示例
public class Demo {
public static void main(String []args) {
String str = "testdemo";
System.out.println("String: "+str);
int index = str.indexOf("demo");
System.out.printf("Substring 'demo' is at index %d
", index);
}
}输出
String: testdemo Substring 'demo' is at index 4
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP