wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes last IndexOf method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "last IndexOf method" Watch "last IndexOf method" New topic
Author

last IndexOf method

Basanti Mathad
Ranch Hand

Joined: Aug 27, 2002
Posts: 60
Hi,
can anyone tell me what wud be the result of this method and why? I thought the answer is
-1.Is that right?
String banner = "One man, One vote";
int subInd1 = banner.lastIndexOf("One", 10);
System.out.println(subInd1);
Thank you
Sridhar Srikanthan
Ranch Hand

Joined: Jan 08, 2003
Posts: 366
Originally posted by Basanti Mathad:
Hi,
can anyone tell me what wud be the result of this method and why? I thought the answer is
-1.Is that right?
String banner = "One man, One vote";
int subInd1 = banner.lastIndexOf("One", 10);
System.out.println(subInd1);
Thank you

According to java lang API,
public int lastIndexOf(String str,
int fromIndex)
Returns the index within this string of the last occurrence of the specified substring, searching backward starting at the specified index. The integer returned is the largest value k such that:

So it starts at index 10 which happens to be n in the second one in the string and searches backwords to find out the match for string "One". As there is a One starting at index 9, it returns 9. If you were to call banner.lastIndexOf("One",8 ), then the value is 0
Hope this helps
Sri
 
Consider Paul's rocket mass heater.
 
subject: last IndexOf method
 
Similar Threads
compile-time polymorphism
A question about thread safety and servlets
A question about thread safety and servlets
how to get object of compiler class
is this a Valid method declaration and definition