Win a copy of Head First Android this week in the Android forum!
  • 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
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

indexOf

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String str = "Java Jives, Java Jives Java";
int i4 = str.indexOf("Jives", 4);
int i5 = str.lastIndexOf("Jives", 5);
System.out.println(i4);
System.out.println(i5);
The output is 5 and 5.Can anynody explain the output?
 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
indexOf(String str, int fromIndex)
Returns the index within this string of the first occurrence of the specified substring, starting at the specified index.
"Jives" is found at index 5 so the output is 5.
If you set the fromIndex as 5 above, it still returns 5.

lastIndexOf(int ch, int fromIndex)
Returns the index within this string of the last occurrence of the specified character, searching backward
starting at the specified index.
Begins to search backward from index 5 but since "Jives" is found at index 5, it returns 5.
If you set the fromIndex as 4, "Jives" is not found hence returns -1.
I was confused by looking at the code too, but after running the code with different index values, I arrived at this conclusion.
 
Smith
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
still iam not clear of lastIndexOf
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi, Smith
this kind of problem you can take a look of API doc.
i try to explain:
first, indexOf() is search from start and lastIndexOf() is search from end.
second, indexOf() is find the lowest value as str.startWith("Jives", 4) && k>=4(according to doc ), so find String str is "Jives, Java Jives Java" and result is 5;
lastIndexOf() is find the largest value as str.startWith("Jives", 5) && k<=5, so find String str is "Java J", it can find at positoin 5->J, so result is 5;
hop this can help
michael
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
'Smith'
PROPER NAMES ARE NOW REQUIRED!!
Read this post for more details.
Ajith
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic