Solved

lastIndexOf method in java

Posted on 2014-12-02
7
390 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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

This is about my first experience with programming Arduino.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Suggested Courses

751 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