Java StringBuffer lastIndexOf()方法
java.lang.StringBuffer.lastIndexOf(String str, int fromIndex)方法返回指定子字符串最后一次出现的字符串中的索引fromIndex参数是从中开始搜索的索引。
1 语法
public int lastIndexOf(String str, int fromIndex)
2 参数
str : 这是要搜索的子字符串。
fromIndex : 这是开始搜索的索引。
3 返回值
此方法返回指定子字符串最后一次出现的此序列中的索引。
4 示例
package com.yiidian;
/**
* 一点教程网: http://www.yiidian.com
*/
/**
* Java Enum compareTo()方法
*/
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