zoukankan      html  css  js  c++  java
  • 查找字符串最后一次出现的位置

    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
Copyright © 2011-2022 走看看