我无法理解Java 11中的lastindexof方法-版本2。

huangapple go评论61阅读模式
英文:

I cant understand this lastindexof method in Java 11- version2

问题

以下是代码的翻译部分:

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

        String str = "this is how";

        int a0 = str.lastIndexOf("is");

        int a = str.lastIndexOf("is", 0);
        int b = str.lastIndexOf("is", 1);
        int c = str.lastIndexOf("is", 2);
        int d = str.lastIndexOf("is", 3);
        int e = str.lastIndexOf("is", 4);
        int f = str.lastIndexOf("is", 5);

        int b1 = str.lastIndexOf("is", -1);
        int c1 = str.lastIndexOf("is", -2);
        int d1 = str.lastIndexOf("is", -3);
        int e1 = str.lastIndexOf("is", -4);
        int f1 = str.lastIndexOf("is", -5);

        System.out.println("从索引0开始的最后出现位置 " + a0);

        System.out.println("从索引0开始的最后出现位置 " + a);
        System.out.println("从索引1开始的最后出现位置 " + b);
        System.out.println("从索引2开始的最后出现位置 " + c);
        System.out.println("从索引3开始的最后出现位置 " + d);
        System.out.println("从索引4开始的最后出现位置 " + a);
        System.out.println("从索引5开始的最后出现位置 " + f);
        System.out.println("从索引-1开始的最后出现位置 " + b1);
        System.out.println("从索引-2开始的最后出现位置 " + c1);
        System.out.println("从索引-3开始的最后出现位置 " + d1);
        System.out.println("从索引-4开始的最后出现位置 " + e1);
        System.out.println("从索引-5开始的最后出现位置 " + f1);
    }
}

结果:

从索引0开始的最后出现位置 5
从索引0开始的最后出现位置 -1
从索引1开始的最后出现位置 -1
从索引2开始的最后出现位置 2
从索引3开始的最后出现位置 2
从索引4开始的最后出现位置 -1
从索引5开始的最后出现位置 5
从索引-1开始的最后出现位置 -1
从索引-2开始的最后出现位置 -1
从索引-3开始的最后出现位置 -1
从索引-4开始的最后出现位置 -1
从索引-5开始的最后出现位置 -1
英文:
public class Test2{
public static void main(String[] args) {
String str="this is how";
int a0=str.lastIndexOf("is");
int a=str.lastIndexOf("is",0);
int b=str.lastIndexOf("is",1);
int c=str.lastIndexOf("is",2);
int d=str.lastIndexOf("is",3);
int e=str.lastIndexOf("is",4);
int f=str.lastIndexOf("is",5);
int b1=str.lastIndexOf("is",-1);
int c1=str.lastIndexOf("is",-2);
int d1=str.lastIndexOf("is",-3);
int e1=str.lastIndexOf("is",-4);
int f1=str.lastIndexOf("is",-5);
System.out.println("Last index of from index 0 "+a0);
System.out.println("Last index of from index 0 "+a);
System.out.println("Last index of from index 1 "+b);
System.out.println("Last index of from index 2 "+c);
System.out.println("Last index of from index 3 "+d);
System.out.println("Last index of from index 4 "+a);
System.out.println("Last index of from index 5 "+f);
System.out.println("Last index of from index -1 "+b1);
System.out.println("Last index of from index -2 "+c1);
System.out.println("Last index of from index -3 "+d1);
System.out.println("Last index of from index -4 "+e1);
System.out.println("Last index of from index -5 "+f1);
}

}

Results

Last index of from index 0 5
Last index of from index 0 -1
Last index of from index 1 -1
Last index of from index 2 2
Last index of from index 3 2
Last index of from index 4 -1
Last index of from index 5 5
Last index of from index -1 -1
Last index of from index -2 -1
Last index of from index -3 -1
Last index of from index -4 -1
Last index of from index -5 -1

答案1

得分: 1

int lastIndexOf(str, fromIndex): 返回字符串中最后一次出现的子字符串 str,从指定的索引 fromIndex 开始向后搜索。

示例:

String char1 = 't';
String str = "test";

int index = str.lastIndexOf(char1); // 将返回 index = 3
int index_b = str.lastIndexOf(char1, 2); // 将从字母 's' 开始向后搜索,返回 index_b = 0

详细文档可以在这里找到。

英文:

int lastIndexOf(str, fromIndex): Returns the last occurrence of str, starts searching backward from the specified index “fromIndex”.

Example:

String char1 = 't';
String str = "test";
int index = str.lastIndexOf(char1); // Will return index = 3
int index_b = str.lastIndexOf(char1, 2) //Will start search backwards from letter 's' and return index_b = 0 

It's well documented here

huangapple
  • 本文由 发表于 2020年7月30日 19:38:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/63172394.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定