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

