I need help on this: Write your own versions of String search methods indexOf and lastIndexOf.

Hope you could help.thanks.

9 Years
Discussion Span
Last Post by stultuske

I don't think so, but java docs can. Check the java docs for the methods and try implement the methods yourself those are some pretty basic ones.


i'm a beginner on programing that is why i dont know much about this.


maybe you don't know much (yet) but this is exactly the reason you are given assignments like this, to get to know the syntaxis and regulary used methods and to create your own implementation of them.

asking us to just give you the code would mean that in a week, you still have to say "I don't know much, which is why I need help".

try to understand what indexOf() and lastIndexOf() do:
a String - object can be seen as an array of chars, so the indexOf("substring") will give you the 'index' of the start of the given substring within the original String.

for instance:

String ob = "the first String";
System.out.println("place: " + ob.indexOf("St"));
System.out.println("place: " + ob.indexOf("notPresent"));

just run this code, see what it does, and interprete the results you get.

I take it that lastIndexOf("") will be quite clear after the answers you've already been given.

once you figured out what the code I've posted above does, it should be quite easy to write a new implementation of this methods

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.