Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

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
SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start 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.
Avatar of gudii9

ASKER

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
I don't know that indexOf should be called firstIndexOf, but you can cetainly think of it that way.
Avatar of gudii9

ASKER

yea
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.