Questions tagged [stringbuffer]

StringBuffer in Java is used as a thread-safe, mutable sequence of characters, replaced by StringBuilder in Java 5.0+

A thread-safe, mutable sequence of characters. A string buffer is like a String, but can be modified. At any point in time it contains some particular sequence of characters, but the length and content of the sequence can be changed through certain method calls.

In Java 5.0, it was replaced by StringBuilder as this was faster, and there was almost not good use case for a thread safe StringBuffer and classes which attempted to use it as such ended up not being thread safe anyway. e.g. SimpleDateFormat.

The Javadoc for StringBuffer notes

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.

485 questions
8
votes
4 answers

Correctly over-loading a stringbuf to replace cout in a MATLAB mex file

MathWorks currently doesn't allow you to use cout from a mex file when the MATLAB desktop is open because they have redirected stdout. Their current workaround is providing a function, mexPrintf, that they request you use instead. After googling…
user27315
  • 538
  • 5
  • 8
8
votes
6 answers

Java: StringBuffer & Concatenation

I'm using StringBuffer in Java to concat strings together, like so: StringBuffer str = new StringBuffer(); str.append("string value"); I would like to know if there's a method (although I didn't find anything from a quick glance at the…
Jean Paul Galea
  • 1,321
  • 4
  • 18
  • 26
8
votes
1 answer

stringstream->rdbuf()->pubsetbuf is not setting the buffer

I am trying to modify a stringbuffer of a stringstream object without having to copy a string, using the method pubsetbuf, but it is not working. I am following the documentation in http://www.cplusplus.com/reference/iostream/streambuf/pubsetbuf/.…
7
votes
2 answers

Why StringBuffer has a toStringCache while StringBuilder not?

In JDK 8, StringBuffer class has a toStringCache, while StringBuilder doesn't. /** * A cache of the last value returned by toString. Cleared * whenever the StringBuffer is modified. */ private transient char[] toStringCache; But why? One…
ntysdd
  • 1,206
  • 2
  • 9
  • 19
7
votes
3 answers

replaceAll does not replace string

I want the text "REPLACEME" to be replaced with my StringBuffer symbols. When I print symbols, it is a valid string. When I print my query, it still has the text REPLACEME instead of symbols. Why? private String buildQuery(){ String query =…
Sheehan Alam
  • 60,111
  • 124
  • 355
  • 556
7
votes
5 answers

Should I use StringBuilder or StringBuffer for webapps?

I a writing a webapp in Java 1.6 and running it in tomcat. While I am not doing any explicit threading, I wonder about what is going on behind the scenes with Spring and Tomcat. Would I run into any issues using StringBuilder instead of…
Bob Roberts
  • 539
  • 8
  • 22
6
votes
2 answers

StringBuilder / StringBuffer with literal string in memory

example code: StringBuffer sb = new StringBuffer("hi"); sb = null; Question: will the literal string "hi" somehow stay in memory, even after the StringBuffer has been garbage collected? Or is it just used to create a char array for the…
Artanis
  • 561
  • 1
  • 7
  • 26
6
votes
2 answers

Java regex match markdown syntax for headings

I have a string with markdown syntax in it, and I want to be able to find markdown syntax for headings, i.e h1 = #, h2 = ## etc etc. I know that whenever I find a heading, it is at the start of the line. I also know there can only be one heading per…
Kaffemakarn
  • 119
  • 1
  • 6
6
votes
2 answers

On Java 1.7+ should we still need to convert "this string" + "should" + "be" + "joined" using StringBuffer.append for best practices?

On Java 1.7+ should we still need to convert "this string" + "should" + "be" + "joined" using StringBuffer.append for best practices?
kjv.007
  • 93
  • 10
6
votes
6 answers

A Set in java never allows duplicates, but it takes StringBuffer objects with the same argument. Why?

public static void main(String[] args) { HashSet set = new HashSet(); set.add(new StringBuffer("abc")); set.add(new StringBuffer("abc")); set.add(new StringBuffer("abc")); set.add(new StringBuffer("abc")); …
Nitesh
  • 107
  • 2
  • 6
6
votes
4 answers

Max size for StringBuffer

Why would the StringBuffer have a limit on its size? I went through some of the links : http://www.coderanch.com/t/540346/java/java/maximum-size-hold-String-buffer. Is that because of the count member variable, which is an int? Suppose that we have…
vjk
  • 2,163
  • 6
  • 28
  • 42
5
votes
3 answers

Get the index of the start of a newline in a Stringbuffer

I need to get the starting position of new line when looping through a StringBuffer. Say I have the following document in a stringbuffer "This is a test Test Testing Testing" New lines exist after "test", "Test" and "Testing". I need something…
Decrypter
  • 2,784
  • 12
  • 38
  • 57
5
votes
2 answers

fixed-length StringBuffer in java

what is the best practise to hold a stringbuffer length fixed in java ? That is if the fixed value is 10 and stringbuffer holds ABCDEFGHIJ, when we append K that will cause A to be cleared and resulting value will be BCDEFGHIJK.I am thinking to use…
cacert
  • 2,677
  • 10
  • 33
  • 56
5
votes
2 answers

Difference between length() and capacity() methods in StringBuilder

I was trying to figure out when to use or why capacity() method is different from length() method of StringBuilder or StringBuffer classes. I have searched on Stack Overflow and managed to come up with this answer, but I didn't understand its…
user8562091
5
votes
1 answer

How can I improve performance, if append() is called on StringBuffer (or StringBuilder) consecutively without reusing the target variable

I have the following piece of code in Java. String foo = " "; Method 1: StringBuffer buf = new StringBuffer(); buf.append("Hello"); buf.append(foo); buf.append("World"); Method 2: StringBuffer buf = new…
manoj
  • 131
  • 2
  • 8
1 2
3
32 33