-8

我对为什么posOfDot返回 15 感到困惑:

for(String l2 : list2){

    System.out.println("l2FileNAme: "+l2);
    int posOfI= l2.indexOf("_")+1;
    System.out.println("posOfI: "+posOfI);

    String l2FileName = l2.substring(0,posOfI);
    System.out.println("l2FileNAme: "+l2FileName);

    for(String l1 : list1){

        int posOfDot= l2.indexOf(".");
        System.out.println("posOfDot: "+posOfDot);
        //substring the root of the file
        //String l1FileName = l1.substring(0,posOfDot); //fails here as posOfDot exceeds string length in substring.
        System.out.println("l1FileNAme: "+l1);  
    }
}

输出:

l2FileNAme: scenario8_items.txt
posOfI: 10
l2FileNAme: scenario8_
posOfDot: 15
l1FileNAme: scenario8_.csv

我希望 posOfDot 为 10,就像 posOfI 一样,但它返回 15。怎么会?

我试过的:

  • 最后一个索引
  • 指数
  • 字符表示法和字符串表示法('' 和 "")

- 编辑 -

每个列表(list1 和 list2)都包含一个文件名列表。这些不是实际的文件本身,而只是文件名。

4

1 回答 1

4
l2FileNAme: scenario8_items.txt

算上屏幕上的字符,.它排在第 16 位,在从零开始的索引中是 15。您正在执行此操作l2

对我来说似乎是正确的输出。

于 2013-05-07T20:20:44.463 回答