StringBuffer Class in the java.lang Package
StringBuffer Constructors
| Constructor | Description |
|---|---|
| StringBuffer() | Default constructor. Creates an object that can initially store 16 characters. |
| StringBuffer(int capacity) | Creates an object that can store the number of characters specified by capacity. |
| StringBuffer(String str) | Creates an object that can store the string specified by str plus 16 additional characters. |
Main StringBuffer Methods
| Method | Description |
|---|---|
| StringBuffer append(boolean b) | Adds b to the end of the current string. |
| StringBuffer append(char c) | Adds c to the end of the current string. |
| StringBuffer append(double d) | Adds d to the end of the current string. |
| StringBuffer append(float f) | Adds f to the end of the current string. |
| StringBuffer append(int i) | Adds i to the end of the current string. |
| StringBuffer append(long l) | Adds l to the end of the current string. |
| StringBuffer append(Object obj) | Adds obj to the end of the current string. |
| StringBuffer append(String str) | Adds str to the end of the current string. |
| int capacity() | Returns the current string buffer capacity. |
| char charAt(int index) | Returns the character at index. |
| StringBuffer delete(int start, int end) | Deletes the range from start to end. |
| StringBuffer deleteCharAt(int index) | Deletes the character at index. |
| StringBuffer insert(int offset, boolean b) | Inserts b before offset. |
| StringBuffer insert(int offset, char c) | Inserts c before offset. |
| StringBuffer insert(int offset, int i) | Inserts i before offset. |
| StringBuffer insert(int offset, long l) | Inserts l before offset. |
| StringBuffer insert(int offset, Object obj) | Inserts obj before offset. |
| StringBuffer insert(int offset, String str) | Inserts str before offset. |
| int length() | Returns the number of characters in the character buffer. |
| StringBuffer replace(int start, int end, String str) | Replaces the range from start to end with the string str. |
| StringBuffer reverse() | Returns the string in reverse order. |
| void setCharAt(int index, char ch) | Sets the character at index to ch. |
| void setLength(int newLength) | Sets the buffer size to newLength. |
| String toString() | Returns the current string as a String object. |
String Buffer Size
- int capacity()
- int length()
- void setLength(int newLength)
The capacity method is used to get the total capacity allocated to a StringBuffer object.
The length method is used to get the current length of a StringBuffer object.
Example
package com.devkuma.tutorial.stringbuffer;
public class StringBufferCapacity {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer("abcdef");
System.out.println("capacity : " + sb.capacity());
System.out.println("length : " + sb.length());
sb.setLength(2);
System.out.println("setLength : " + sb);
}
}
Execution result:
capacity : 22
length : 6
setLength : ab
In this result, the length method displays 6, the length of “abcdef”, while the capacity method displays 22 because it automatically adds space for 16 more characters to the 6-character length of “abcdef”. The setLength method changes the buffer size to 2, so “ab” is displayed.
Characters in a String Buffer
Characters in a StringBuffer object can be read and replaced.
- char charAt(int index)
- void setCharAt(int index, char ch)
package com.devkuma.tutorial.stringbuffer;
public class StringBufferChar {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer("abcde");
System.out.println("charAt(3) : " + sb.charAt(3));
sb.setCharAt(4, 'f');
System.out.println("setCharAt(4, 'f') : " + sb);
}
}
Execution result:
charAt(3) : d
setCharAt(4, 'f') : abcdf
First, charAt displays the value ’d’ at index 3. Second, setCharAt changes the value at index 4 to ‘f’, so the final result abcdf is displayed.
Appending Data to a String Buffer
To convert data of another type to a string and append it to the end of a StringBuffer object, use the append method.
- StringBuffer append(boolean b)
- StringBuffer append(char c)
- StringBuffer append(double d)
- StringBuffer append(float f)
- StringBuffer append(int i)
- StringBuffer append(long l)
- StringBuffer append(Object obj)
- StringBuffer append(String str)
The following example uses append.
package com.devkuma.tutorial.stringbuffer;
public class StringBufferAppend {
public static void main(String[] args) {
boolean b = true;
char c = 'a';
double d = 1.1;
float f = 3.14f;
int i = 1;
long l = 2;
String str = "A";
StringBuffer sb = new StringBuffer();
sb.append(b).append(", ");
sb.append(c).append(", ");
sb.append(d).append(", ");
sb.append(f).append(", ");
sb.append(i).append(", ");
sb.append(l).append(", ");
sb.append(str);
System.out.println(sb);
}
}
Execution result:
true, a, 1.1, 3.14, 1, 2, A
The append method has a similar purpose to concat on a String object. Unlike String, however, the changed string is stored in the called object instead of being assigned to a new object. When many operations append another string to a given string, using a StringBuffer object greatly helps performance.
The following example creates a String object and a StringBuffer object, then appends a string 10000 times to each. Let’s measure the speed of each object. The measurement uses System.currentTimeMillis(), a method that gets the current time.
package com.devkuma.tutorial.stringbuffer;
public class StringBufferAppend2 {
public static void main(String[] args) {
String str = "A";
long currentStr = System.currentTimeMillis();
for (int i = 0; i < 10000; i++) {
str += "A";
}
System.out.println("String :" + (System.currentTimeMillis() - currentStr) + "ms");
StringBuffer sb = new StringBuffer("A");
long currentSb = System.currentTimeMillis();
for (int i = 0; i < 10000; i++) {
sb.append("A");
}
System.out.println("StringBuffer :" + (System.currentTimeMillis() - currentSb) + "ms");
}
}
Execution result:
String :68ms
StringBuffer :1ms
From the result, you can see that the StringBuffer object is much faster than the String object.
Inserting Data into a String Buffer
To insert data of another type as a string at a specified position in a StringBuffer object, use the insert method.
- StringBuffer insert(int offset, boolean b)
- StringBuffer insert(int offset, char c)
- StringBuffer insert(int offset, int i)
- StringBuffer insert(int offset, long l)
- StringBuffer insert(int offset, Object obj)
- StringBuffer insert(int offset, String str)
package com.devkuma.tutorial.stringbuffer;
public class StringBufferInsert {
public static void main(String[] args) {
boolean b = true;
char c = 'a';
double d = 1.1;
float f = 3.14f;
int i = 1;
long l = 2;
String str = "A";
StringBuffer sb = new StringBuffer("b=, c=, d=, f=, i=, l=, str=");
sb.insert(2, b);
sb.insert(10, c);
sb.insert(15, d);
sb.insert(22, f);
sb.insert(30, i);
sb.insert(35, l);
sb.insert(42, str);
System.out.println(sb);
}
}
Execution result:
b=true, c=a, d=1.1, f=3.14, i=1, l=2, str=A
Reversing a String Buffer
Returns the string in a StringBuffer object in reverse order.
- StringBuffer reverse()
package com.devkuma.tutorial.stringbuffer;
public class StringBufferReverse {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer("abcdef");
System.out.println(sb.reverse());
}
}
Execution result:
fedcba
Deleting from a String Buffer
The delete method of a StringBuffer object deletes the specified range.
The deleteCharAt method of a StringBuffer object deletes the character at the entered index.
- StringBuffer delete(int start, int end)
- StringBuffer deleteCharAt(int index)
package com.devkuma.tutorial.stringbuffer;
public class StringBufferDelete {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer("abcxxxdef");
sb.delete(4, 6);
System.out.println(sb);
sb.deleteCharAt(3);
System.out.println(sb);
}
}
The execution result is as follows.
abcxdef
abcdef
Replacing in a String Buffer
In the string of a StringBuffer object, replaces the specified range with the input string.
- StringBuffer replace(int start, int end, String str)
package com.devkuma.tutorial.stringbuffer;
public class StringReplace {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer("abcxxghi");
sb.replace(3, 5, "def");
System.out.println(sb);
}
}
The execution result is as follows.
abcdefghi