This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
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. regards maulin
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. "Java" 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
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.
Joined: Apr 16, 2003
Anupam, 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 -1 7 7
Joined: Apr 13, 2003
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.