• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Question about lastIndexOf() method

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can some one please explain me the answer and reason?

String str = "One man, One Vote";
int sub = str.lastIndexOf("One" , 10);
System.out.println(sub);
 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answer would be 9.That because lastIndexOf("One",10) returns the position of last occurence of "One" in the String str. 10 is the position in the string from where search starts.

So it starts searching backwards from 10th position, until it finds the char sequence "One". Once it finds it, it return the index where the "One" starts in the given string.
Try this sample code for better understanding :


}

See how the index changes based on the value of second argument,which is the fromPoint for starting the search.
 
anmita payal
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Vikrant, still can you clarify me this one.

public static void main(String args[]) {
String str = "One man, One vote";
int index = str.lastIndexOf("One",8);
System.out.println(index) ;
index = str.lastIndexOf("One",5);
System.out.println(index) ;
}

when I tried this I am getting 0 and 0.
It starts searching backwards from 8th position means is it starting from index 0 or 1 from right side?
 
Vikrant Sahdev
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anmita
First of all , you have a space after comma,which is messing up your answer.
If you try the same code with "One man,One vote", you will get
int index = str.lastIndexOf("One",8); // output 8, bcos it matches second One
int index = str.lastIndexOf("One",5); // output 0, bcos it matches first One

Once again, it starts search backwards from positions 8 and 5. Whenever it encounters the first match going backwards ( which is last occurence of the sequence "One"), it reports the position of that match w.r.t its position in string based on index 0 from begining of string.

So essentially indexOf, searches forward and reports the first occurence of string pattern. While lastIndexOf,searches backwards and reports the last occurence of the string pattern, giving its position in the string with index 0 starting from the left.
Hope that clears your doubt !
 
And inside of my fortune cookie was this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic