How can we re assign the value of a StringBuffer or StringBuilder Variable?
StringBuffer sb=new StringBuffer("teststr");
Now i have to change the value of sb to “testString” without emptying the contents.
I am looking at a method which can do this assignment directly without using separate memory allocation.I think we can do it only after emptying the contents.
It should first be mentioned that
StringBuilderis generally preferred toStringBuffer. FromStringBuffer‘s own API:That said, I will stick to
StringBufferfor the rest of the answer because that’s what you’re asking; everything thatStringBufferdoes,StringBuilderalso… except synchronization, which is generally unneeded. So unless you’re using the buffer in multiple threads, switching toStringBuilderis a simple task.The question
So you want
sbto have theStringvalue"testString"in its buffer? There are many ways to do this, and I will list some of them to illustrate how to use the API.The optimal solution: it performs the minimum edit from
"teststr"to"testString". It’s impossible to do it any faster than this.This needlessly overwrites
"tr"with"tr".This involves shifts due to
deleteCharAtandinsert.This is a bit different now: it doesn’t magically know that it has
"teststr"that it needs to edit to"testString"; it assumes only that theStringBuffercontains at least one occurrence of"str"somewhere, and that it needs to be replaced by"String".Let’s say now that you want to replace ALL occurrences of
"str"and replace it with"String". AStringBufferdoesn’t have this functionality built-in. You can try to do it yourself in the most efficient way possible, either in-place (probably with a 2-pass algorithm) or using a secondStringBuffer, etc.But instead I will use the
replace(CharSequence, CharSequence)fromString. This will be more than good enough in most cases, and is definitely a lot more clear and easier to maintain. It’s linear in the length of the input string, so it’s asymptotically optimal.Discussions
The exact memory location shouldn’t really be a concern for you; in fact, both
StringBuilderandStringBufferwill reallocate its internal buffer to different memory locations whenever necessary. The only way to prevent that would be toensureCapacity(or set it through the constructor) so that its internal buffer will always be big enough and it would never need to be reallocated.However, even if
StringBufferdoes reallocate its internal buffer once in a while, it should not be a problem in most cases. Most data structures that dynamically grows (ArrayList,HashMap, etc) do them in a way that preserves algorithmically optimal operations, taking advantage of cost amortization. I will not go through amortized analysis here, but unless you’re doing real-time systems etc, this shouldn’t be a problem for most applications.Obviously I’m not aware of the specifics of your need, but there is a fear of premature optimization since you seem to be worrying about things that most people have the luxury of never having to worry about.