int indexOf(String str, int fromIndex)
描述 (Description)
java.lang.String.indexOf(String str, int fromIndex)方法从指定索引处开始返回指定子字符串第一次出现的字符串中的索引。 返回的整数是最小值k,其中 -
k 》 = Math.min(fromIndex, this.length()) && this.startsWith(str, k)
如果不存在这样的k值,则返回-1。 。
声明 (Declaration)
以下是java.lang.String.indexOf()方法的声明public int indexOf(String str, int fromIndex)
参数 (Parameters)str - 这是要搜索的子字符串。
fromIndex - 这是开始搜索的索引。
返回值 (Return Value)
此方法返回指定子字符串第一次出现的字符串中的索引,从指定的索引开始。
异常 (Exception)
NA
例子 (Example)
以下示例显示了java.lang.String.indexOf()方法的用法。package com.iowiki;
import java.lang.*;
public class StringDemo {
public static void main(String[] args) {
String str1 = "Collections of tutorials at tutorials point";
/* search starts from index 10 and if located it returns
the index of the first character of the substring "tutorials" */
System.out.println("index = " + str1.indexOf("tutorials", 10));
/* search starts from index 9 and returns -1 as substring "admin"
is not located */
System.out.println("index = " + str1.indexOf("admin", 9));
}
}
让我们编译并运行上面的程序,这将产生以下结果 -index = 15
index = -1