File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Mughal String.lastIndexOf Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Mughal String.lastIndexOf" Watch "Mughal String.lastIndexOf" New topic

Mughal String.lastIndexOf

Sadhi Mushrif

Joined: May 09, 2003
Posts: 11
This queation os from Mughal.
String a ="Java Jives";
int i = s.lastIndexOf('J',4);
If you print variable i it's value is 0.
I was wondering why it was not 5 ?
Thanks in advance.
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1873
hi sadhi
well, because its "FROM LAST INDEX".
we use lastIndexOf() so the index lookup will start from the last character in the string. so if we say lastIndexOf('J',4) it mean,s
1. start looking for 'J' from last character in the string
2. we have to start lookup skipping "last" 4 characters that are "Jives" where 'J' in "Jives" is the 4th one.
and so the latest 'J' after 'J' in "Jive" from BACK is "Java"'s 'J'. NOw, the actual index of that 'J' in the string is 0th. so it returns 0.
hope you got it.
Ryan Wilson
Ranch Hand

Joined: Apr 16, 2003
Posts: 65

This is what I think is going on...
the lastIndexOf(String s, int fromIndex) is going to compare fromIndex and s.length(). Its going to take the min of the two.
In the case above
fromIndex = 4 and s.length() = 10
fromIndex is less so it is going to create a sub-string from the String beginning to fromIndex.
The lastIndexOf will then return the index of the last occurance of the string passed in. (returning -1 if the String is not found)
J is in position zero.
Try changing the fromIndex to 6.
The output will be 5
The sub-String is now "Java J"
I hope this helps
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1090
I don't get the head and tail of this. I am just not able to understand how lastIndexof() method works.
Consider this

Why is the out put -1.
Ryan Wilson
Ranch Hand

Joined: Apr 16, 2003
Posts: 65
I'm not sure if my previous post was correct or not, but try this out. To me it appears to work the way that I described above. However everyone else states that it counts backwards?
Can someone please clear this up.

The output will be
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1090
Yes Wilson you are correct. The lastIndexOf(int ch, int till) method of the String class probably functions in the following way. When you pass in the first agrument as an int that int is converted to a char. Then the second argument is the no. of characters you want from the String to be searched(simply a substring where the search would be done). Now that substring, is searched backwards or you can say the position of the last occurence of the first agrument(the char to be searched) is returned.
For example consider a String "1234567890654321"(name the above String str) now when I say str.lastIndexOf('6',3). The first agrument is '6' which means the char 6. Now a substring is created containing the first four(an agrument of 3 means 4 elements because the counting starts from 0) elements namely(1,2,3,4) and the char(6) is searched. Which ofcourse is not present. So the method returns -1. But if we use str.lastIndexOf('6',5) the substring that would be created would contain the elements(1,2,3,4,5,6) in which case the element is found. Now the location that is returned is the actual location of the character in the string, not in respect to any method parameter or so. In this case as 6 is in the fifth position so 5 is returned. In case there are two or more characters present in a string and the same char is used for the search then the last index of the char in the substring that is created would be returned. For example, "123123123" in this case if I search for 3 using "123123123".lastIndexOf('3',6) the new substring would be 1231231 and the position where last three occurs is 5 so the returned value is 5.
The substring creation mentioned in this post is just to explain how it works. I am not sure what approach java uses, whatever the approach is the answer is the same. So no probs.
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391

[ May 22, 2003: Message edited by: Marlene Miller ]
I agree. Here's the link:
subject: Mughal String.lastIndexOf
It's not a secret anymore!