• 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
  • Paul Clapham
  • Tim Cooke
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Junilu Lacar
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst
  • Peter Rooke
  • Mikalai Zaikin
Bartenders:
  • Himai Minh

Mughal String.lastIndexOf

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This queation os from Mughal.
String a ="Java Jives";
012345
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.
 
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sadhi

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
 
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Anupam Sinha
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

[ May 22, 2003: Message edited by: Marlene Miller ]
 
Hey, check out my mega multi devastator cannon. It's wicked. It makes this tiny ad look weak:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic