目录

int lastIndexOf(String str)

描述 (Description)

java.lang.StringBuilder.lastIndexOf(String str)方法返回指定子字符串最右边出现的字符串中的索引。

声明 (Declaration)

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

public int lastIndexOf(String str)

参数 (Parameters)

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

返回值 (Return Value)

如果字符串参数作为此对象中的子字符串出现一次或多次,则此方法返回返回的最后一个子字符串的第一个字符的索引。如果它不作为子字符串出现,则返回-1。

异常 (Exception)

NullPointerException - 如果str为null。

例子 (Example)

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

package com.iowiki;
import java.lang.*;
public class StringBuilderDemo {
   public static void main(String[] args) {
      StringBuilder str = new StringBuilder("tutorials point");
      System.out.println("string = " + str);
      /* returns the index within this string of the rightmost occurrence 
         of the specified substring */
      System.out.println("last Index of a = " + str.lastIndexOf("a"));
      // returns -1 as substring am is not found
      System.out.println("last index of am = " + str.lastIndexOf("am"));
   }
}  

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

string = tutorials point
last Index of a = 6
last index of am = -1
↑回到顶部↑
WIKI教程 @2018