java.lang.StringBuffer.lastIndexOf(String str)方法實例
java.lang.StringBuffer.lastIndexOf(String str) 方法返回該字符串指定的子串的最右邊出現處的索引。
聲明
以下是**java.lang.StringBuffer.lastIndexOf()**方法的聲明
public int lastIndexOf(String str)
參數
- str -- 這是要搜索的字符串。
返回值
如果出現字符串參數1次或以上在這個對象的子字符串,然後將最後一個這樣的子字符串的第一個字符索引返回。如果找不到這樣的一個子串,則返回-1。
異常
- NullPointerException -- 如果 str 是 null.
例子
下面的例子顯示java.lang.StringBuffer.lastIndexOf()方法的使用。
package com.yiibai; import java.lang.*; public class StringBufferDemo { public static void main(String[] args) { StringBuffer buff = new StringBuffer("tutorials yiibai"); 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 yiibai last index of a = 6 last index of am = -1