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




    int b1="abcd".indexOf("d",-1);
    int c1="abcd".indexOf("d",-2);
    int d1="abcd".indexOf("d",-3);
    int e1="abcd".indexOf("d",-4);
    int f1="abcd".indexOf("d",-5);


    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 3
Last index of from index 1 3
Last index of from index 2 3
Last index of from index 3 3
Last index of from index 4 3
Last index of from index 5 -1
Last index of from index -1 3
Last index of from index -2 3
Last index of from index -3 3
Last index of from index -4 3
Last index of from index -5 3

Can someone explain how positive and negative index work?

alex K
  • 81
  • 1
  • 1
  • 5
  • For your purposes have a look at [lastIndexOf](https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#lastIndexOf(java.lang.String,%20int)) because the [indexOf](https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#indexOf(java.lang.String,%20int)) works differently – Sync it Jul 30 '20 at 03:47
  • 0-4 is the starting index it'll look for the char d and return 3. 5 is outside the array bounds and will return -1. Any negatives basically start from index 0. – Arundeep Chohan Jul 30 '20 at 03:49
  • Oops . My question is about lastindexof. – alex K Jul 30 '20 at 11:22

1 Answers1

1

It is not spelled out for indexOf(String, int), but indexOf(int, int) behaves the same way (emphasis mine):

There is no restriction on the value of fromIndex. If it is negative, it has the same effect as if it were zero: this entire string may be searched. If it is greater than the length of this string, it has the same effect as if it were equal to the length of this string: -1 is returned.

(Note that your text talks about lastIndexOf, but the code is indexOf. The logic for lastIndexOf is reversed: whereas the whole string is found after any negative index, none of the string is in front of a negative index, so lastIndexOf with a negative starting point will never find anything.)

Amadan
  • 191,408
  • 23
  • 240
  • 301
  • Ok my mistake. My question is lastindexof – alex K Jul 30 '20 at 11:22
  • 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); – alex K Jul 30 '20 at 11:34
  • 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); – alex K Jul 30 '20 at 11:34
  • 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); – alex K Jul 30 '20 at 11:34
  • 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 Process finished with exit code 0 – alex K Jul 30 '20 at 11:34
  • Delete this question. Added new modified – alex K Jul 30 '20 at 11:39
  • https://stackoverflow.com/questions/63172394/i-cant-understand-this-lastindexof-method-in-java-11-version2 – alex K Jul 30 '20 at 11:39
  • I explained for `lastIndexOf` as well. Please read my answer again. There is nothing special about negative starting position. Also, if you need to add extra information, edit the question, don:t add 3 code comments. – Amadan Jul 30 '20 at 20:32