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中方法重載和方法重寫的區別
下一篇:無