wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes String Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "String" Watch "String" New topic
Author

String

Gaurav Pavan Kumar Jain
Ranch Hand

Joined: Mar 19, 2007
Posts: 168
Below code is confusing me please help me to understand

Stringbuffer s="123456789"

s.delete(0,3)
s.replace(1,3,"24")

I cannot understand what will be the output
Lucia Short
Greenhorn

Joined: Aug 19, 2007
Posts: 13
In the code snippet:

StringBuffer s= new StringBuffer("123456789");
s.delete(0,3);
System.out.println(s);
s.replace(1,3,"24");
System.out.println(s);

The output is:
456789
424789

because in both functions delete(int start, int end) and replace(int start, int end), the start index is 0-based, but the end index is 1-based.
Gaurav Pavan Kumar Jain
Ranch Hand

Joined: Mar 19, 2007
Posts: 168
Thank you very much for your help
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14117
    
  16

Originally posted by Lucia Short:
...the start index is 0-based, but the end index is 1-based.

That's not the way you should look at it. Look at it this way: Both indices are 0-based, but the begin index is inclusive, and the end index is exclusive.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Kelvin Chenhao Lim
Ranch Hand

Joined: Oct 20, 2007
Posts: 513
Originally posted by Jesper Young:

That's not the way you should look at it. Look at it this way: Both indices are 0-based, but the begin index is inclusive, and the end index is exclusive.

Yeah, this is the only part of K&B that really made me shake my head when I first read it. What were they thinking when they wrote this "end index is 1-based" explanation? Not only is it more confusing than the "end index exclusive" explanation, but the official API documentation itself uses the "end index exclusive" terminology. (Bert, if you're around, I'd love to hear your thoughts on this. )


SCJP 5.0
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String