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

Hope you could help.thanks.

Recommended Answers

All 4 Replies

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.

Help has already been offered if care enough to look for it.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.