Solved

lastIndexOf method in java

Posted on 2014-12-02
7
388 Views
Last Modified: 2014-12-02
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
0
Comment
Question by:gudii9
  • 3
  • 2
  • 2
7 Comments
 
LVL 84

Assisted Solution

by:ozo
ozo earned 250 total points
ID: 40476157
"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
0
 
LVL 32

Accepted Solution

by:
awking00 earned 250 total points
ID: 40476782
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"
0
 
LVL 84

Expert Comment

by:ozo
ID: 40476847
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.
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 7

Author Comment

by:gudii9
ID: 40476939
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
0
 
LVL 32

Expert Comment

by:awking00
ID: 40476965
I don't know that indexOf should be called firstIndexOf, but you can cetainly think of it that way.
0
 
LVL 7

Author Comment

by:gudii9
ID: 40476970
yea
0
 
LVL 84

Expert Comment

by:ozo
ID: 40476991
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.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question