目录

int lastIndexOf(String str, int fromIndex)

描述 (Description)

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

声明 (Declaration)

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

public int lastIndexOf(String str, int fromIndex)

参数 (Parameters)

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

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

返回值 (Return Value)

此方法返回指定子字符串最后一次出现的此序列中的索引。

异常 (Exception)

NullPointerException - 如果str为null。

例子 (Example)

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

package com.iowiki;
import java.lang.*;
public class StringBufferDemo {
   public static void main(String[] args) {
      StringBuffer buff = new StringBuffer("tutorials point");
      System.out.println("buffer = " + buff);
      /* returns the index within this string of the rightmost occurrence of the
         specified substring */
      System.out.println("last index of i = " + buff.lastIndexOf("i"));
      // returns index as the substring is found with fromIndex 10
      System.out.print("last index of als = ");
      System.out.println(buff.lastIndexOf("als",10));
      // returns -1 as fromIndex 2 can't find the substring 
      System.out.print("last index of ori = "); 
      System.out.println(buff.lastIndexOf("ori",2));  
   }
}

让我们编译并运行上面的程序,这将产生以下结果 -

buffer = tutorials point
last index of i = 12
last index of als = 6
last index of ori = -1
↑回到顶部↑
WIKI教程 @2018