首页 > 其他 > 详细

查找字符串最后一次出现的位置

时间:2021-08-29 19:14:54      阅读:11      评论:0      收藏:0      [点我收藏+]
package font_files;

public class SearchLastString {
    public static void main(String[] args) {
       String randomword = "nice is good, and I like nicessss, do you like?";
       String love ="nice";
       int location = randomword.lastIndexOf(love);
       if (location==-1){
           System.out.println("Not find!");
       }else {
           System.out.println("randomword is located in " + location);
       }
    }
}

结果为:

randomword is located in 25

技术分享图片

可以看出,这个lastIndexOf()函数只是找到相同的字符段就行了,不管是不是相同的单词。




package font_files;

public class SearchLastString {
    public static void main(String[] args) {
       String randomword = "nice is good, and I like neicessss, do you like?";
       String love ="nice";
       int location = randomword.lastIndexOf(love);
       if (location==-1){
           System.out.println("Not find!");
       }else {
           System.out.println("randomword is located in " + location);
       }
    }
}

结果为:

randomword is located in 0

这个查找是从0 开始的。

查找字符串最后一次出现的位置

原文:https://www.cnblogs.com/blueblog6/p/15202816.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!