Solved

lastIndexOf method in java

Posted on 2014-12-02
7
393 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
Part One of the two-part Q&A series with MalwareTech.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Suggested Courses

627 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