Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

SubString Index query  RSS feed

 
Ranch Hand
Posts: 1083
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?



 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!