• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Problem with StringBuffer

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hallo!

I have a problem with a Java code that uses the StringBuffer class.
My source code is like this:

public String replaceStr(String veryLongString)
{
StringBuffer buf = new StringBuffer(veryLongString); // veryLongString = 2178
buf.replace(1096,1780,"Test"); // buf = 2180
}

Why is buf 2 characters longer than veryLongString?




 
Ranch Hand
Posts: 954
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

buf.replace(1096,1780,"Test"); // buf = 2180


You are replacing 784 characters long string with 4 characters long string "Test" and it still returning you buffer length 2180..
 
Håkan Axheim
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tushar Goel wrote:

buf.replace(1096,1780,"Test"); // buf = 2180


You are replacing 784 characters long string with 4 characters long string "Test" and it still returning you buffer length 2180..



Wrong --> buf.replace(1096,1100,"Test");
 
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
How are you measuring how many characters are in buf? Are you using the capacity() method or the length() method?

You should be using length(); capacity() only tells you how large the internal buffer of the StringBuffer is, not how many characters it currently holds.

From your code, I would expect that the number of characters in buf after replace() would be veryLongString.length() - (1780 - 1096) + 4 = veryLongString.length() - 680 characters, and not veryLongString.length() + 2 characters.
 
Saloon Keeper
Posts: 14511
325
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On a tangent, is there a reason you're using a StringBuffer and not a StringBuilder?
 
We don't have time to be charming! Quick, read this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic