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

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

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

上一篇:LeetCode101.对称二叉树


下一篇:使用vue进行表单数据验证