gudii9
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));
}
}
i wrote my program as above and got below output
string.indexOf("wex",5) -> 7
string.indexOf("wex",9) -> 16
string.indexOf("wex",strin
string.lastIndexOf("wex",1
string.lastIndexOf("wex",1
string.lastIndexOf("wex",7
string.lastIndexOf("wex",1
string.lastIndexOf("wex",1
I as not clear on output
string.lastIndexOf("wex",1
string.lastIndexOf("wex",1
string.lastIndexOf("wex",7
string.lastIndexOf("wex",1
string.lastIndexOf("wex",1
How and why it came for lastIndexOf().
Please advise
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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.
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.
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.
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.