當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Java Java.lang.StringBuffer.lastIndexOf()用法及代碼示例


描述

這個java.lang.StringBuffer.lastIndexOf(String str, int fromIndex) 方法返回此字符串中最後一次出現的指定子字符串的索引。參數fromIndex是開始搜索的索引。

聲明

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

public int lastIndexOf(String str, int fromIndex)

參數

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

  • fromIndex- 這是開始搜索的索引。

返回值

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

異常

NullPointerException- 如果 str 為空。

示例

下麵的例子展示了 java.lang.StringBuffer.lastIndexOf() 方法的用法。

package com.tutorialspoint;

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

相關用法


注:本文由純淨天空篩選整理自 Java.lang.StringBuffer.lastIndexOf() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。