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


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


描述

這個java.lang.StringBuffer.lastIndexOf(String str) 方法返回此字符串中最右邊出現的指定子字符串的索引。

聲明

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

public int lastIndexOf(String str)

參數

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

返回值

如果字符串參數作為此對象中的子字符串出現一次或多次,則返回最後一個此類子字符串的第一個字符的索引。如果它不是作為子字符串出現,則返回 -1。

異常

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 a = " + buff.lastIndexOf("a"));

      // returns -1 as substring am is not found
      System.out.println("last index of am = " + buff.lastIndexOf("am"));
   }
}

讓我們編譯並運行上麵的程序,這將產生以下結果 -

buffer = tutorials point
last index of a = 6
last index of am = -1

相關用法


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