Java String.lastIndexOf(str, index) is out of my understanding - Stack Overflow

the Java function String.lastIndexOf(str, index) is for me not fully clear.Let's take easy exampl

the Java function String.lastIndexOf(str, index) is for me not fully clear. Let's take easy example

      String a1 = "1231231";
      String s1 = "23";
      int i1 = a1.length() - 3; //=4
      int x = a1.lastIndexOf(s1, i1);
      System.out.println(x); //will output 4

the main string has the length of 7 chars in that example, looking also for string with 2 chars length by specific index but it outputs found string at position 4, where i have no real idea how it gets computed by Java since it should search backwards and not forwards. As in example

           //0123456 
String a1 = "1231231";

at 4th index there is char 2 but since it should search backwards and not forwards so the next expected index is 1 and not 4, but like it looks Java is going out of searching backwards but looks sometimes forwards too like in this example even if limited by index.

I have checked sources .java#L1793

    public int lastIndexOf(String str) {
        return lastIndexOf(str, value.length);
    }

evidently, if no index provided then as index gets used full length of the main string. For that example that would be 7 chars. In such case assumed that index plays the role of string size limiter but it was wrong assumption too. Then assumed 7 char long string - 3 = 4 chars long string, then it should probably trim from right 3 chars and search backwards.

           //0123456 
String a1 = "1231231";

but that was wrong assumption too. In fact i wanted to get faster way of searching with lastIndexOf in main string but with limiting the size of main string without trimming it by String.substring(). But Java's function String.lastIndexOf(str, value.length); seems to work with its own specific logic that doesn't allow this kind of logic even if firstly assumed by checking sources that this parameter lastIndexOf(str, value.length); points to maximal length of main string. I don't say really, that the behavior is out of my understanding, highly here is more or less the question is in what situation this function could be required with index?

the Java function String.lastIndexOf(str, index) is for me not fully clear. Let's take easy example

      String a1 = "1231231";
      String s1 = "23";
      int i1 = a1.length() - 3; //=4
      int x = a1.lastIndexOf(s1, i1);
      System.out.println(x); //will output 4

the main string has the length of 7 chars in that example, looking also for string with 2 chars length by specific index but it outputs found string at position 4, where i have no real idea how it gets computed by Java since it should search backwards and not forwards. As in example

           //0123456 
String a1 = "1231231";

at 4th index there is char 2 but since it should search backwards and not forwards so the next expected index is 1 and not 4, but like it looks Java is going out of searching backwards but looks sometimes forwards too like in this example even if limited by index.

I have checked sources https://github/bpupadhyaya/openjdk-8/blob/master/jdk/src/share/classes/java/lang/String.java#L1793

    public int lastIndexOf(String str) {
        return lastIndexOf(str, value.length);
    }

evidently, if no index provided then as index gets used full length of the main string. For that example that would be 7 chars. In such case assumed that index plays the role of string size limiter but it was wrong assumption too. Then assumed 7 char long string - 3 = 4 chars long string, then it should probably trim from right 3 chars and search backwards.

           //0123456 
String a1 = "1231231";

but that was wrong assumption too. In fact i wanted to get faster way of searching with lastIndexOf in main string but with limiting the size of main string without trimming it by String.substring(). But Java's function String.lastIndexOf(str, value.length); seems to work with its own specific logic that doesn't allow this kind of logic even if firstly assumed by checking sources that this parameter lastIndexOf(str, value.length); points to maximal length of main string. I don't say really, that the behavior is out of my understanding, highly here is more or less the question is in what situation this function could be required with index?

Share Improve this question asked Mar 2 at 21:04 sam sussam sus 211 bronze badge 2
  • 4 From the docs: Returns the index within this string of the last occurrence of the specified substring It's not the last index of the backwards String. – Andrew S Commented Mar 2 at 21:17
  • 1 "in what situation this function could be required with index?" : the index can be used to search the 2nd (3rd, ...) occurrence (eventually) after finding the 1st one -- example "1231231".lastIndexOf("23") results in 4; so if we want to find the 2nd to last occurrence, we can call "1231231".lastIndexOf("23", 4-1) – user85421 Commented Mar 2 at 22:33
Add a comment  | 

2 Answers 2

Reset to default 4

The substring is always matched in the forward direction for String#indexOf or String#lastIndexOf. Therefore, a1.lastIndexOf(s1) and a1.lastIndexOf(s1, a1.lastIndexOf(s1)) will always return the same index.

String a1 = "1231231";
String s1 = "23";
int lastIndex = a1.lastIndexOf(s1); // 4
System.out.println(lastIndex);
System.out.println(a1.lastIndexOf(s1, lastIndex)); // 4
int secondLastIndex = a1.lastIndexOf(s1, lastIndex - 1); //1
System.out.println(secondLastIndex);

It sounds to me like you are interpreting last index as previous index where it's actually the index of the last occurrence (or perhaps final occurrence) of that substring within the source string. If the substring's last position starts at n then as long as the fromIndex position is between 0 and n inclusive, you will return n.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745118376a4612268.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信