my dog learned polymorphism*
The moose likes Java in General and the fly likes SubString Index query Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "SubString Index query" Watch "SubString Index query" New topic
Author

SubString Index query

Vishal Hegde
Ranch Hand

Joined: Aug 01, 2009
Posts: 1055

Hi,

How is the line

being read. The String "mail: " is it being counted as 0,1,2,3,4,5,6(Index Position) or only 0. If its considering only 0 why is that so?




http://www.lifesbizzare.blogspot.com || OCJP:81%
Thomas Kasene
Greenhorn

Joined: Feb 21, 2013
Posts: 3
You should start at the inner-most method call:
When you call indexOf() on a String (in this case, "mail: vishal.hegde@xyz.com"), you get the index of the first occurrance of "mail: " in that string. Since "mail: " is at the beginning, indexOf() returns 0.

Then there's the next method call:
As you probably noticed, I replaced the call to indexOf() with the result to make it easier to read. Calling substring() on a String returns a part of that string. Since you're using the String.substring( int ) version of the method, you get the part of the string starting at index 6 (0+6 = 6). Let's see which part that is:
According to the code block above, substring() should return the string " vishal.hegde@xyz.com". Note that the extra whitespace is preserved at the beginning of the string.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: SubString Index query
 
Similar Threads
Why does Font.getFont return null when looking for system font?
Possible error in question from Kathy Sierray, Bert Bates SCJP6 book
Read prev line in the file?
awt radio button
Cannot understand the program