17

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.

Arsaceus
  • 293
  • 2
  • 19
Biju CD
  • 4,999
  • 11
  • 34
  • 55
  • Can you explain more of what is it that you want to achieve? And is it with `StringBuffer` particularly and you will not accept the newer, faster, better `StringBuilder` instead? – polygenelubricants Apr 12 '10 at 05:01
  • In Java the **assignment** operator is `=` and the **reassignment** term is usually applied to scope variables (e.g. `sb = new StringBuffer()`). So, generally speaking, your question is a bit incorrect, because the only answer you can get is `sb = new StringBuffer("testString")` -- you probably wouldn't ask a bountified question about such sort of things. And the phrase "without emptying the contents" brings total confusion. So please take your time to explain your goals better. – BorisOkunskiy Apr 12 '10 at 05:12
  • @polygenelubricants StringBuilder is ok.But am looking for the method in StringBuilder to assign value later by using previous. memory location. – Biju CD Apr 12 '10 at 05:26
  • @cdb: see discussion section in my answer. – polygenelubricants Apr 12 '10 at 05:48
  • @cdb, please explain further your issue with using replace(). – Marcus Adams Apr 15 '10 at 13:32
  • @Marcus Adams No Issue in using replace other than its position arguments. – Biju CD Apr 19 '10 at 05:37

8 Answers8

29
sb.setLength(0);
sb.append("testString");
Mr. Shiny and New 安宇
  • 13,822
  • 6
  • 44
  • 64
webjockey
  • 1,647
  • 2
  • 20
  • 28
17

It should first be mentioned that StringBuilder is generally preferred to StringBuffer. From StringBuffer's own API:

As of release JDK 5, this class has been supplemented with an equivalent class designed for use by a single thread, StringBuilder. The StringBuilder class should generally be used in preference to this one, as it supports all of the same operations but it is faster, as it performs no synchronization.

That said, I will stick to StringBuffer for the rest of the answer because that's what you're asking; everything that StringBuffer does, StringBuilder also... except synchronization, which is generally unneeded. So unless you're using the buffer in multiple threads, switching to StringBuilder is a simple task.


The question

StringBuffer sb = new StringBuffer("teststr");

"Now i have to change the value of sb to "testString" without emptying the contents"

So you want sb to have the String value "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.

StringBuffer sb = new StringBuffer("teststr");
sb.setCharAt(4, 'S');
sb.append("ing");
assert sb.toString().equals("testString");

This needlessly overwrites "tr" with "tr".

StringBuffer sb = new StringBuffer("teststr");
sb.replace(4, sb.length(), "String");
assert sb.toString().equals("testString");

This involves shifts due to deleteCharAt and insert.

StringBuffer sb = new StringBuffer("teststr");
sb.deleteCharAt(4);
sb.insert(4, 'S');
sb.append("ing");
assert sb.toString().equals("testString");

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 the StringBuffer contains at least one occurrence of "str" somewhere, and that it needs to be replaced by "String".

StringBuffer sb = new StringBuffer("strtest");
int idx = sb.indexOf("str");
sb.replace(idx, idx + 3, "String");
assert sb.toString().equals("Stringtest");

Let's say now that you want to replace ALL occurrences of "str" and replace it with "String". A StringBuffer doesn'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 second StringBuffer, etc.

But instead I will use the replace(CharSequence, CharSequence) from String. 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.

String before = "str1str2str3";
String after = before.replace("str", "String");
assert after.equals("String1String2String3");

Discussions

"I am looking for the method to assign value later by using previous memory location"

The exact memory location shouldn't really be a concern for you; in fact, both StringBuilder and StringBuffer will reallocate its internal buffer to different memory locations whenever necessary. The only way to prevent that would be to ensureCapacity (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 StringBuffer does 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.

polygenelubricants
  • 376,812
  • 128
  • 561
  • 623
  • yes know its a kind of premature optimization but am asking this because this is a hot topic in interviews. – Biju CD Apr 12 '10 at 10:54
  • *"The exact memory location shouldn't really be a concern for you;"* -- true, but if a method took a parameter `void func(StringBuilder sb)` you might want to clear that exact parameter. `sb.setLength(0);` will do here, but `sb = new StringBuilder();` won't -- of course. – towi Dec 06 '13 at 13:46
  • "The exact memory location shouldn't really be a concern for you": well, if I just reassign `StringBuilder` variable like this: `sb = new StringBuilder(newValue);` then I won't be able to pass this variable in lambda function because "variable must be final or effectively final". – izogfif Dec 03 '20 at 10:42
8

What do you mean with "reassign"? You can empty the contents by using setLength() and then start appending new content, if that's what you mean.

Edit: For changing parts of the content, you can use replace().

Generally, this kind of question can be easily answered by looking at the API doc of the classes in question.

Michael Borgwardt
  • 342,105
  • 78
  • 482
  • 720
  • 1
    Plus, I'm pretty sure `replace()` does a copy of the new string into StringBuilder storage. I don't think you can do this with no memory allocation. – sblom Apr 12 '10 at 05:28
  • @sblom: if the StringBuilder's underlying array is long enough to hold the replaced string, ther shouldn't be any allocation necessary. – Michael Borgwardt Apr 12 '10 at 08:35
3

You can use a StringBuilder in place of a StringBuffer, which is typically what people do if they can (StringBuilder isn't synchronized so it is faster but not threadsafe). If you need to initialize the contents of one with the other, use the toString() method to get the string representation. To recycle an existing StringBuilder or StringBuffer, simply call setLength(0).

Edit
You can overwrite a range of elements with the replace() function. To change the entire value to newval, you would use buffer.replace(0,buffer.length(),newval). See also:

Michael Aaron Safyan
  • 93,612
  • 16
  • 138
  • 200
2

You might be looking for the replace() method of the StringBuffer:

StringBuffer sb=new StringBuffer("teststr");
sb.replace(0, sb.length() - 1, "newstr");

Internally, it removes the original string, then inserts the new string, but it may save you a step from this:

StringBuffer sb=new StringBuffer("teststr");
sb.delete(0, sb.length() - 1);
sb.append("newstr");

Using setLength(0) reassigns a zero length StringBuffer to the variable, which, I guess, is not what you want:

StringBuffer sb=new StringBuffer("teststr");
// Reassign sb to a new, empty StringBuffer
sb.setLength(0);
sb.append("newstr");
Marcus Adams
  • 53,009
  • 9
  • 91
  • 143
1

Indeed, I think replace() is the best way. I checked the Java-Source code. It really overwrites the old characters.

Here is the source code from replace():

public AbstractStringBuffer replace(int start, int end, String str)
  {
    if (start < 0 || start > count || start > end)
      throw new StringIndexOutOfBoundsException(start);

    int len = str.count;
    // Calculate the difference in 'count' after the replace.
    int delta = len - (end > count ? count : end) + start;
    ensureCapacity_unsynchronized(count + delta);

    if (delta != 0 && end < count)
      VMSystem.arraycopy(value, end, value, end + delta, count - end);

    str.getChars(0, len, value, start);
    count += delta;
    return this;
  }
Martijn Courteaux
  • 67,591
  • 47
  • 198
  • 287
1

Changing entire value of StringBuffer:

  StringBuffer sb = new StringBuffer("word");
  sb.setLength(0); // setting its length to 0 for making the object empty
  sb.append("text");

This is how you can change the entire value of StringBuffer.

SM ANSARI
  • 335
  • 3
  • 13
-1

You can convert to/from a String, as follows:

 StringBuffer buf = new StringBuffer();
 buf.append("s1");
 buf.append("s2");

 StringBuilder sb = new StringBuilder(buf.toString());
 // Now sb, contains "s1s2" and you can further append to it
Itay Maman
  • 30,277
  • 10
  • 88
  • 118