xxxxxxxxxx
// A StringBuffer is a mutable sequence of characters. It is used to create and manipulate strings.
// Unlike String class, StringBuffer provides methods to modify the contents of the string without creating a new object.
// Here's an example of using StringBuffer to concatenate multiple strings:
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append("Hello ");
stringBuffer.append("World!");
System.out.println(stringBuffer.toString()); // Output: Hello World!
xxxxxxxxxx
// Java Program showcasing some of StringBuffer class capabilities
import java.io.*;
class StringBufferDemo {
public static void main(String[] args) {
// Creating StringBuffer object
StringBuffer s = new StringBuffer("Dummy value");
// Getting the length of object
int length = s.length();
// Getting the capacity of object
int capacity = s.capacity();
// Printing the length and capacity of
System.out.println("Length of string buffer = "
+ length); // 11
System.out.println(
"Capacity of string buffer = " + capacity); // 27
// append method
s.append(" appended");
System.out.println(s); // Dummy value appended
// insert method
s.insert(s.length(), " for illustration.");
System.out.println(s); // Dummy value appended for illustration.
// delete method
s.delete(0, 6);
System.out.println(s); // value appended for illustration.
// deleteCharAt method
s.deleteCharAt(0);
System.out.println(s); // alue appended for illustration.
// reverse method
s.reverse();
System.out.println(s); // .noitartsulli rof dedneppa eula
}
}
xxxxxxxxxx
Internet Explorer is the only browser which really suffers from this in today's world. (Versions 5, 6, and 7 were dog slow. 8 does not show the same degradation.) What's more, IE gets slower and slower the longer your string is.
If you have long strings to concatenate then definitely use an array.join technique. (Or some StringBuffer wrapper around this, for readability.) But if your strings are short don't bother.