Java StringBuffer lastIndexOf()方法及示例

来源:这里教程网 时间:2026-02-17 21:13:40 作者:

Java StringBuffer lastIndexOf()方法及示例

在StringBuffer类中,有两种类型的lastIndexOf()方法,取决于传递给它的参数。

lastIndexOf(String str)

StringBuffer类的 lastIndexOf(String str) 方法是一个内置的方法,用于返回最后出现的子串作为参数在String中的索引。最后出现的空字符串””被认为是在索引值this.length()处出现的。如果子串str不存在,那么将返回-1。

语法 :

public int lastIndexOf(String str)

参数: 该方法只接受一个参数 str ,它是一个字符串类型的值,指的是我们想得到的最后出现的字符串的索引。

返回值: 该方法返回所传递的子串的最后出现的 索引 ,如果没有这样的子串,则返回-1。

下面的程序说明了java.lang.StringBuffer.lastIndexOf()方法。

例1: 当传递的子串在序列中存在时。

// Java program to demonstrate// the lastIndexOf() Method.  class GFG {    public static void main(String[] args)    {          // create a StringBuffer object        // with a String pass as parameter        StringBuffer str            = new StringBuffer("GeeksForGeeks");          // print string        System.out.println("String contains = " + str);          // get index of string For        int index = str.lastIndexOf("Geeks");          // print results        System.out.println("Index of last occurrence"                           + " string \"Geeks\"= "                           + index);    }}

输出:

String contains = GeeksForGeeksIndex of last occurrence string "Geeks"= 8

例子2: 当传递的子串在序列中不存在时。

// Java program to demonstrate// the lastIndexOf() Method.  class GFG {    public static void main(String[] args)    {          // create a StringBuffer object        // with a String pass as parameter        StringBuffer str            = new StringBuffer(                "Geeks for Geeks contribute");          // print string        System.out.println("String contains = " + str);          // get index of string article        int index = str.lastIndexOf("article");          // print results        System.out.println("Index of string"                           + " 'article' = "                           + index);    }}

输出:

String contains = Geeks for Geeks contributeIndex of string 'article' = -1

lastIndexOf(String str, int fromIndex)

StringBuffer类的 lastIndexOf(String str, int fromIndex) 方法是一个内置的方法,用于返回字符串中第一次出现的子串参数的索引,从指定的索引’fromIndex’开始向后搜索。如果子串str不存在,则返回-1。 fromIndex 是整数类型的值,指的是开始搜索的索引,但是这个搜索是从索引’fromIndex’开始向后搜索。这个方法返回的索引是从序列的开始计算的,唯一不同的是在这个方法中给出了搜索开始的索引。如果字符串出现在搜索开始的索引之后而不是之前,那么将返回-1。

语法

public int lastIndexOf(String str, int fromIndex)

参数: 该方法接受两个参数。

str 是字符串类型的值,指的是要获取索引的字符串。fromIndex ,整数类型的值,指的是从哪个索引开始向后搜索。

返回: 该方法返回从指定索引开始的最后出现的子串的 索引 ,如果没有这样的子串,则返回-1。

下面的程序说明了StringBuffer.lastIndexOf()方法。

例1: 当传递的子串在序列中存在时。

// Java program to demonstrate// the lastIndexOf() Method.  class GFG {    public static void main(String[] args)    {          // create a StringBuffer object        // with a String pass as parameter        StringBuffer str            = new StringBuffer("GeeksForGeeks");          // print string        System.out.println("String contains = " + str);          // get index of string Form index 3        int index = str.lastIndexOf("For", 8);          // print results        System.out.println("index of last occurrence"                           + " string \"For\" = "                           + index);    }}

输出:

String contains = GeeksForGeeksindex of last occurrence string "For" = 5

例2: 当fromIndex小于子串的最后出现的索引时

// Java program to demonstrate// the lastIndexOf() Method.  class GFG {    public static void main(String[] args)    {          // create a StringBuffer object        // with a String pass as parameter        StringBuffer str            = new StringBuffer("Geeks for Geeks contribute");          // print string        System.out.println("String contains = " + str);          // get index of string Geeks from index 15        int index = str.lastIndexOf("contribute", 10);          // print results        System.out.println("index of string"                           + " 'contribute ' = "                           + index);    }}

输出:

String contains = Geeks for Geeks contributeindex of string 'contribute ' = -1

参考资料:

https://docs.oracle.com/javase/10/docs/api/java/lang/StringBuffer.html#lastIndexOf(java.lang.String, int)https://docs.oracle.com/javase/10/docs/api/java/lang/StringBuffer.html#lastIndexOf(java.lang.String)

相关推荐