Java StringBuffer int lastIndexOf(String s,int srcindex)方法,带示例

StringBuffer类int lastIndexOf(String s,int srcindex) (StringBuffer Class int lastIndexOf(String s , int srcindex))

  • This method is available in package java.lang.StringBuffer.lastIndexOf(String s, int srcindex).

    包java.lang.StringBuffer.lastIndexOf(String s,int srcindex)中提供了此方法。

  • This method is used to return the index of the last occurrence of the specified substring which will be searched in a StringBuffer object and srcindex(Searching index) is the index to start the search from.

    此方法用于返回将在StringBuffer对象中搜索的指定子字符串最后一次出现的索引,而srcindex(Searching index)是从其开始搜索的索引。

  • This method returns -1 if substring is not found in the given StringBuffer object.

    如果在给定的StringBuffer对象中找不到子字符串,则此方法返回-1。

Syntax:

句法:

    int lastIndexOf(String s, int srcindex){
    }

Parameter(s):

参数:

We pass only two objects in the method of the StringBuffer i.e. String (s) and searching index(srcindex). Here String is those characters sequence which we want to search and srcindex is the index to start the search from.

我们仅在StringBuffer方法中传递两个对象,即字符串和搜索index(srcindex) 。 在这里,字符串是我们要搜索的字符序列,而srcindex是开始搜索的索引。

Return value:

返回值:

The return type of this method is int that means this method returns the index of the last occurrence of specified substring which will be search in a StringBuffer object and i.e. index is in integer form that's why return type is int.

此方法的返回类型为int ,这意味着该方法返回指定子字符串的最后一次出现的索引,该索引将在StringBuffer对象中搜索,即index为整数形式,这就是返回类型为int的原因。

Java程序演示lastIndexOf(String s,int srcindex)方法的示例 (Java program to demonstrate example of lastIndexOf(String s , int srcindex) method)

Case 1: How int lastIndexOf(String s, int srcindex) method works with duplicate(Multiple times) substring in the StringBuffer object?

情况1:int lastIndexOf(String s,int srcindex)方法如何与StringBuffer对象中的重复(多次)子字符串一起使用?

import java.lang.StringBuffer;

public class StringBufferClass {
    public static void main(String[] args) {

        StringBuffer sb = new StringBuffer(" java.utilisapackageofJava ");

        // use lastIndexOf(String s , int srcindex) 
        // it will retrieve the index of last occurrence 
        // of specified substring from the StringBuffer object .

        // Display result after implementing lastIndexOf("i""",9 )i.e. 
        // "i" comes twice till the searching of 9th index in a 
        // stringbuffer object so it returns the index of 
        // last occurrence of "i" i.e. 9th index
        // [ First index of "i" is 7th index and Second index of "i" is 9th]
        System.out.println("The index of last occurrence of i is :" + sb.lastIndexOf("i", 9));
    }
}

Output

输出量

The index of last occurrence of i is :8

Case 2: How int lastIndexOf(String s) method works without duplicate(i.e. Multiple times) substring in the StringBuffer object?

情况2:int lastIndexOf(String s)方法如何在StringBuffer对象中没有重复的(即多次)子字符串的情况下工作?

import java.lang.StringBuffer;

public class StringBufferClass {
    public static void main(String[] args) {

        StringBuffer sb = new StringBuffer("Javaispopularinthisworld ");

        // use lastIndexOf(String s) it will retrieve 
        // the index of last occurrence of specified 
        // substring from the StringBuffer object .

        // Display result after implementing lastIndexOf("u")
        // i.e. u comes once till 9th index in a stringbuffer 
        // object so it returns the index of last occurrence of u 
        // i.e. 9th index[ First and Last index of u is one and only one 9th position]
        System.out.println("The index of last occurrence of u is :" + sb.lastIndexOf("u", 9));
    }
}

Output

输出量

The index of last occurrence of u is :9


翻译自: https://www.includehelp.com/java/stringbuffer-int-lastindexof-string-s-int-srcindex-method-with-example.aspx

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值