• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
不用说了,环境决定命运!但只要你不纠缠,亦能渡劫
初中就是这样,垃圾的环境中还是有优秀人物的,紧跟优秀学者
博客园    首页    新随笔    联系   管理    订阅  订阅

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

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 开始的。

热爱每一天
posted @ 2021-08-29 16:06  怎么都比颓废好  阅读(304)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3