This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes Associate Certification (OCAJP 7/8) and the fly likes Substring method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Certification » Associate Certification (OCAJP 7/8)
Bookmark "Substring method" Watch "Substring method" New topic

Substring method

Shane Timlin

Joined: Aug 15, 2013
Posts: 3
Hi, I came across the following question in my revision -


What will be printed by the above code if it is run with command line: java TestClass --0.50 (There are two minuses before 0.)

The explanation says
str.substring(1, str.length()-1) => "--0.50".substring(1, (6-1) ) => -0.5
which still seems too brief for me.
Could somebody explain how I tgets to -0.5?
I see this then gets rounded down to 0 and case 0 & case 1 are applied.


Roel De Nijs

Joined: Jul 19, 2004
Posts: 7247

Please QuoteYourSources and use code-tags for better readibility of the code snippets.

Strings are 0-based and with substring the begin-index is included, the end-index is not.

SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
Have you checked out Aspose?
subject: Substring method
jQuery in Action, 3rd edition