Solved

lastIndexOf method in java

Posted on 2014-12-02
7
380 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 31

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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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 31

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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 …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

757 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now