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
-4
votes
7 answers

How to replace space with hyphen?

I want to replace Space with "-" what is the way Suppose my code is StringBuffer tokenstr=new StringBuffer(); tokenstr.append("technician education systems of the Cabinet has approved the following"); I want output…
Adi
  • 1
  • 1
  • 5
-4
votes
2 answers

Add comment character to lines in a StringBuffer

I have a StringBuffer representing a file. I would like to add comment characters at the beginning of (some of the) lines. For example, if this is how my content looks like: line line line line-to-comment line-to-comment line line I…
danieln
  • 4,795
  • 10
  • 42
  • 64
-4
votes
1 answer

create tableview using html content in android

I have to develop one android native application using stringbuffer with html content. Here i have used below code: PayPalInvoiceItem item1 = new PayPalInvoiceItem(); sb.append(""); for (int i = 0; i <…
android
  • 89
  • 3
  • 12
-5
votes
2 answers

Why this code is not giving the output as expected

I was expecting the output as AB , ABCD but the output is ABCD , B. How's that possible? public class Test { public static void main(String[] args) { StringBuffer a = new StringBuffer("A"); …
DecPK
  • 24,537
  • 6
  • 26
  • 42
-6
votes
1 answer

Java StringBuffer cannot print out apostrophe

I would write a snippet that concats two string(with specific elements) to one. May someone could help me? My code: import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc =…
Bryan C
  • 61
  • 8
1 2 3
32
33