Java.lang.StringBuilder.indexOf() 方法
描述
java.lang.StringBuilder.indexOf(String str, int fromIndex) 方法返回此字符串中第一次出现指定子字符串的索引,从指定索引开始。
声明
以下是 java.lang.StringBuilder.indexOf() 方法的声明。
public int indexOf(String str, int fromIndex)
参数
str − 这是要搜索的子字符串。
fromIndex − 这是开始搜索的索引。
返回值
此方法返回此字符串中第一次出现指定子字符串的索引,从指定索引开始。
异常
NullPointerException − 如果 str 为空。
示例
下面的例子展示了 java.lang.StringBuilder.indexOf() 方法的使用。
package com.tutorialspoint;
import java.lang.*;
public class StringBuilderDemo {
public static void main(String[] args) {
StringBuilder str = new StringBuilder("programming language");
System.out.println("string = " + str);
// returns the index of the specified substring
System.out.println("Index of substring = " + str.indexOf("age"));
/* returns the index of the specified substring, starting at
the specified index */
System.out.println("Index of substring = " + str.indexOf("am",2));
/* returns -1 as the substring is not found starting at the
specified index */
System.out.println("Index of substring = " + str.indexOf("am",10));
}
}
让我们编译并运行上面的程序,这将产生下面的结果 −
string = programming language Index of substring = 17 Index of substring = 5 Index of substring = -1

