lastIndexOf method in java

public class Test35 {

	 * @param args
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		String string="aabbzzzwexrweyyywexooo";//wex is at index 7 and 16
	    System.out.println("string.indexOf(\"wex\",5) -> " + string.indexOf("wex",5));
	    System.out.println("string.indexOf(\"wex\",9) -> " + string.indexOf("wex",9));
	      System.out.println("string.indexOf(\"wex\",string.indexOf(\"wex\",5)+2) -> " + string.indexOf("wex",string.indexOf("wex",5)+2) ); 
	    System.out.println("string.lastIndexOf(\"wex\",16)  ->  " + string.lastIndexOf("wex",16));
	    System.out.println("string.lastIndexOf(\"wex\",11)  -> " + string.lastIndexOf("wex",11));
	    System.out.println("string.lastIndexOf(\"wex\",7)  -> " + string.lastIndexOf("wex",7));
                          System.out.println("string.lastIndexOf(\"wex\",15)  -> " + string.lastIndexOf("wex",15));
                          System.out.println("string.lastIndexOf(\"wex\",17)  -> " + string.lastIndexOf("wex",17));



Open in new window

i wrote my program as above and got below output

string.indexOf("wex",5) -> 7
string.indexOf("wex",9) -> 16
string.indexOf("wex",string.indexOf("wex",5)+2) -> 16
string.lastIndexOf("wex",16)  ->  16
string.lastIndexOf("wex",11)  -> 7
string.lastIndexOf("wex",7)  -> 7
string.lastIndexOf("wex",15)  -> 7
string.lastIndexOf("wex",17)  -> 16

I as not clear on output

string.lastIndexOf("wex",16)  ->  16
string.lastIndexOf("wex",11)  -> 7
string.lastIndexOf("wex",7)  -> 7
string.lastIndexOf("wex",15)  -> 7
string.lastIndexOf("wex",17)  -> 16

How and why it came for lastIndexOf().

Please advise
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

"wex" appears at positions 7 and 16 in string, so
string.lastIndexOf("wex",X) == 16 when 16<=X
string.lastIndexOf("wex",X) == 7 when 7<=X && X<16
string.lastIndexOf("wex",X) == -1 when  X<7
awking00Information Technology SpecialistCommented:
lastIndexOf with 2 parameters (a search string and an index) searches backwards from the index to find the search string.

string.lastIndexOf("wex",16)  ->  16 => OK - Search started at 16 and immediately found "wex"
string.lastIndexOf("wex",11)  -> 7 => OK - Search started at 11, then went to 10, then 9,  then 8,  then 7 and  found "wex"
string.lastIndexOf("wex",7)  -> 7 => OK - Search started at 7 and immediately found "wex"
string.lastIndexOf("wex",15)  -> 7 OK - Search started at 15, then 14, then 13,  then 12,  then 11, then 10, then 9,  then 8,  then 7 and  found "wex"
string.lastIndexOf("wex",17)  -> 16 => OK - Search started at 17, then went to 16 and  found "wex"

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
string.lastIndexOf( str,fromIndex) <= Math.max(fromIndex,-1)

string.lastIndexOf( str,fromIndex) returns the largest value k for which:
     k <= fromIndex && string.startsWith(str, k)

If no such value of k exists, then -1 is returned.
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

gudii9Author Commented:
now lastIndexOf() makes sense.

Similar too how we are calling lastIndexOf() the indexOf() also should be called something like firstIndexOf() since it is finding first occurence index of the target.

Please advise
awking00Information Technology SpecialistCommented:
I don't know that indexOf should be called firstIndexOf, but you can cetainly think of it that way.
gudii9Author Commented:
You can define your own methods If you want,
but when other people read your code they may not be as familiar as the standard methods,
so you should document them at least as clearly as the standard methods,
and be sure that the clarity they add to your code is worth the extra effort to learn your non-standard methods.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.