• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

String

 
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 168
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much for your help
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 513
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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. )
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic