目录

int indexOf(String str, int fromIndex)

描述 (Description)

java.lang.StringBuffer.indexOf(String str, int fromIndex)方法返回从指定索引处开始返回指定子字符串第一次出现的字符串中的索引fromIndex参数是从中开始搜索的索引。

声明 (Declaration)

以下是java.lang.StringBuffer.indexOf()方法的声明

public int indexOf(String str, int fromIndex)

参数 (Parameters)

  • str - 这是要搜索的子字符串。

  • fromIndex - 这是开始搜索的索引。

返回值 (Return Value)

此方法返回指定子字符串第一次出现的字符串中的索引,从指定的索引开始。

异常 (Exception)

NullPointerException - 如果str为null

例子 (Example)

以下示例显示了java.lang.StringBuffer.indexOf()方法的用法。

package com.iowiki;
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
↑回到顶部↑
WIKI教程 @2018