Java String indexOf(String str)
方法将返回指定字符串在字符串中第一次出现的索引,或如果未找到指定子字符串,则返回-1
。
语法
以下是此方法的语法 -
int indexOf(String str)
参数
str
- 要查找的子字符串。
返回值
- 返回
str
所在的索引值。
示例
import java.io.*;
public class Test {
public static void main(String args[]) {
String Str = new String("Welcome to xuhuhu.com");
String SubStr1 = new String("Yii");
System.out.println("Found Index :" + Str.indexOf( SubStr1 ));
}
}
执行上面示例代码,得到以下结果:
Found Index :11
上一篇:
java中方法重载和方法重写的区别
下一篇:无