Java : StringBuffer with Examples

StringBuffer (Java SE 22 & JDK 22) with Examples.
You will find code examples on most StringBuffer methods.


Summary

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.

Class diagram

Please see also : StringBuilder

final var sb = new StringBuffer();
System.out.println(sb); // ""

sb.append("abcd");
System.out.println(sb); // "abcd"

sb.append(1234);
System.out.println(sb); // "abcd1234"

sb.append(true);
System.out.println(sb); // "abcd1234true"

Constructors

StringBuffer ()

Constructs a string buffer with no characters in it and an initial capacity of 16 characters.

final var sb = new StringBuffer();
System.out.println(sb); // ""
System.out.println(sb.capacity()); // 16

sb.append("1234567890123456");
System.out.println(sb); // "1234567890123456"
System.out.println(sb.capacity()); // 16

sb.append("7890");
System.out.println(sb); // "12345678901234567890"
System.out.println(sb.capacity()); // 34

StringBuffer (int capacity)

Constructs a string buffer with no characters in it and the specified initial capacity.

final var sb = new StringBuffer(5);
System.out.println(sb); // ""
System.out.println(sb.capacity()); // 5

sb.append("abcde");
System.out.println(sb); // "abcde"
System.out.println(sb.capacity()); // 5

sb.append("fg");
System.out.println(sb); // "abcdefg"
System.out.println(sb.capacity()); // 12

StringBuffer (CharSequence seq)

Constructs a string buffer that contains the same characters as the specified CharSequence.

final CharSequence seq = new StringBuffer("abc");
System.out.println(seq); // "abc"

final var sb = new StringBuffer(seq);
System.out.println(sb); // "abc"

sb.append("xyz");
System.out.println(sb); // "abcxyz"
System.out.println(seq); // "abc"

StringBuffer (String str)

Constructs a string buffer initialized to the contents of the specified string.

final var sb = new StringBuffer("abc");
System.out.println(sb); // "abc"

sb.append("xyz");
System.out.println(sb); // "abcxyz"

Methods

StringBuffer append (boolean b)

Appends the string representation of the boolean argument to the sequence.

final var sb = new StringBuffer();
System.out.println(sb); // ""

sb.append(true);
System.out.println(sb); // "true"

sb.append(true).append(false);
System.out.println(sb); // "truetruefalse"

StringBuffer append (char c)

Appends the string representation of the char argument to this sequence.

final var sb = new StringBuffer();
System.out.println(sb); // ""

sb.append('a');
System.out.println(sb); // "a"

sb.append('b').append('c');
System.out.println(sb); // "abc"

StringBuffer append (char[] str)

Appends the string representation of the char array argument to this sequence.

final var sb = new StringBuffer();
System.out.println(sb); // ""

final char[] c1 = {'a', 'b', 'c'};
final char[] c2 = {'X', 'Y', 'Z'};
final char[] c3 = {'-', '!'};

sb.append(c1);
System.out.println(sb); // "abc"

sb.append(c2).append(c3);
System.out.println(sb); // "abcXYZ-!"

StringBuffer append (char[] str, int offset, int len)

Appends the string representation of a subarray of the char array argument to this sequence.

final var sb = new StringBuffer();
System.out.println(sb); // ""

final char[] c = {'a', 'b', 'c', 'd', 'e'};

sb.append(c, 0, 2);
System.out.println(sb); // "ab"

sb.append(c, 2, 3).append(c, 4, 1);
System.out.println(sb); // "abcdee"

StringBuffer append (double d)

Appends the string representation of the double argument to this sequence.

final var sb = new StringBuffer();
System.out.println(sb); // ""

sb.append(1.23);
System.out.println(sb); // "1.23"

sb.append(" : ").append(4.567e+20);
System.out.println(sb); // "1.23 : 4.567E20"

StringBuffer append (float f)

Appends the string representation of the float argument to this sequence.

final var sb = new StringBuffer();
System.out.println(sb); // ""

sb.append(1.23f);
System.out.println(sb); // "1.23"

sb.append(" : ").append(4.567e+20f);
System.out.println(sb); // "1.23 : 4.567E20"

StringBuffer append (int i)

Appends the string representation of the int argument to this sequence.

final var sb = new StringBuffer();
System.out.println(sb); // ""

sb.append(-1234);
System.out.println(sb); // "-1234"

sb.append(" : ").append(5678);
System.out.println(sb); // "-1234 : 5678"

StringBuffer append (long lng)

Appends the string representation of the long argument to this sequence.

final var sb = new StringBuffer();
System.out.println(sb); // ""

sb.append(-1234L);
System.out.println(sb); // "-1234"

sb.append(" : ").append(1234567890123456789L);
System.out.println(sb); // "-1234 : 1234567890123456789"

StringBuffer append (CharSequence s)

Appends the specified CharSequence to this sequence.

final CharSequence cs1 = new StringBuilder("abc");
final CharSequence cs2 = new StringBuilder("XYZ");
final CharSequence cs3 = new StringBuilder("-!");

final var sb = new StringBuffer();
System.out.println(sb); // ""

sb.append(cs1);
System.out.println(sb); // "abc"

sb.append(cs2).append(cs3);
System.out.println(sb); // "abcXYZ-!"

StringBuffer append (CharSequence s, int start, int end)

Appends a subsequence of the specified CharSequence to this sequence.

final CharSequence cs = new StringBuffer("abXYZ");

final var sb = new StringBuffer();
System.out.println(sb); // ""

sb.append(cs, 0, 2);
System.out.println(sb); // "ab"

sb.append(" : ").append(cs, 2, 5);
System.out.println(sb); // "ab : XYZ"

StringBuffer append (Object obj)

Appends the string representation of the Object argument.

final var obj1 = LocalDate.of(2100, 4, 16);
final var obj2 = URI.create("https://example.com/");

final var sb = new StringBuffer();
System.out.println(sb); // ""

sb.append(obj1);
System.out.println(sb); // "2100-04-16"

sb.append(" : ").append(obj2);
System.out.println(sb); // "2100-04-16 : https://example.com/"

StringBuffer append (String str)

Appends the specified string to this character sequence.

final var sb = new StringBuffer();
System.out.println(sb); // ""

sb.append("abc");
System.out.println(sb); // "abc"

sb.append("XYZ").append("-!");
System.out.println(sb); // "abcXYZ-!"

StringBuffer append (StringBuffer sb)

Appends the specified StringBuffer to this sequence.

final var buffer1 = new StringBuffer("abc");
final var buffer2 = new StringBuffer("XYZ");
final var buffer3 = new StringBuffer("-!");

final var sb = new StringBuffer();
System.out.println(sb); // ""

sb.append(buffer1);
System.out.println(sb); // "abc"

sb.append(buffer2).append(buffer3);
System.out.println(sb); // "abcXYZ-!"

StringBuffer appendCodePoint (int codePoint)

Appends the string representation of the codePoint argument to this sequence.

final var cp1 = "a".codePointAt(0);
final var cp2 = "b".codePointAt(0);
final var cp3 = "c".codePointAt(0);

System.out.println(Integer.toHexString(cp1)); // 61
System.out.println(Integer.toHexString(cp2)); // 62
System.out.println(Integer.toHexString(cp3)); // 63

final var sb = new StringBuffer();
System.out.println(sb); // ""

sb.appendCodePoint(cp1);
System.out.println(sb); // "a"

sb.appendCodePoint(cp2).appendCodePoint(cp3);
System.out.println(sb); // "abc"
// An example with a surrogate code point.
final var surrogate = "\uD83D\uDE0A"; // "😊" is one character.
System.out.println(surrogate); // "😊"

final var cp = surrogate.codePointAt(0);
System.out.println(Integer.toHexString(cp)); // 1f60a

final var sb = new StringBuffer();
System.out.println(sb); // ""

sb.appendCodePoint(cp);
System.out.println(sb); // "😊"

int capacity ()

Returns the current capacity.

final var sb = new StringBuffer(5);
System.out.println(sb); // ""
System.out.println(sb.capacity()); // 5

sb.append("abcde");
System.out.println(sb); // "abcde"
System.out.println(sb.capacity()); // 5

sb.append("fg");
System.out.println(sb); // "abcdefg"
System.out.println(sb.capacity()); // 12

char charAt (int index)

Returns the char value in this sequence at the specified index.

final var sb = new StringBuffer("abc");
//sb.charAt(-1); // IndexOutOfBoundsException
System.out.println(sb.charAt(0)); // a
System.out.println(sb.charAt(1)); // b
System.out.println(sb.charAt(2)); // c
//sb.charAt(3); // IndexOutOfBoundsException
// An example with a surrogate code point.
final var surrogate = "\uD83D\uDE0A"; // "😊" is one character.
System.out.println(surrogate); // "😊"

final var sb = new StringBuffer(surrogate + "ab");
System.out.println(sb); // "😊ab"

final var ret1 = sb.charAt(0);
System.out.println(ret1); // ? <- Garbled characters.
System.out.println(Integer.toHexString(ret1)); // d83d

final var ret2 = sb.charAt(1);
System.out.println(ret2); // ? <- Garbled characters.
System.out.println(Integer.toHexString(ret2)); // de0a

final var ret3 = sb.charAt(2);
System.out.println(ret3); // a

final var ret4 = sb.charAt(3);
System.out.println(ret4); // b

//sb.charAt(4); // IndexOutOfBoundsException

IntStream chars ()

Returns a stream of int zero-extending the char values from this sequence.

final var sb = new StringBuffer("abcd");

sb.chars().forEach(c -> {
    System.out.printf("%x : %c%n", c, c);
});

// Result
// ↓
// "61 : a"
// "62 : b"
// "63 : c"
// "64 : d"
// An example with a surrogate code point.
final var surrogate = "\uD83D\uDE0A"; // "😊" is one character.
System.out.println(surrogate); // "😊"

final var sb = new StringBuffer("ab" + surrogate);
System.out.println(sb); // "ab😊"

sb.chars().forEach(c -> {
    System.out.printf("%x : %c%n", c, c);
});

// Result
// ↓
// "61 : a"
// "62 : b"
// "d83d : ?" <- Garbled characters.
// "de0a : ?" <- Garbled characters.

int codePointAt (int index)

Returns the character (Unicode code point) at the specified index.

final var sb = new StringBuffer("abc");
//sb.codePointAt(-1); // IndexOutOfBoundsException
System.out.printf("%c%n", sb.codePointAt(0)); // a
System.out.printf("%c%n", sb.codePointAt(1)); // b
System.out.printf("%c%n", sb.codePointAt(2)); // c
//sb.codePointAt(3); // IndexOutOfBoundsException
// An example with a surrogate code point.
final var surrogate = "\uD83D\uDE0A"; // "😊" is one character.
System.out.println(surrogate); // "😊"

final var sb = new StringBuffer(surrogate + "ab");
System.out.println(sb); // "😊ab"

final var ret1 = sb.codePointAt(0);
System.out.printf("%c%n", ret1); // 😊
System.out.println(Integer.toHexString(ret1)); // 1f60a

final var ret2 = sb.codePointAt(1);
System.out.printf("%c%n", ret2); // ? <- Garbled characters.
System.out.println(Integer.toHexString(ret2)); // de0a

final var ret3 = sb.codePointAt(2);
System.out.printf("%c%n", ret3); // a

final var ret4 = sb.codePointAt(3);
System.out.printf("%c%n", ret4); // b

//sb.codePointAt(4); // IndexOutOfBoundsException

int codePointBefore (int index)

Returns the character (Unicode code point) before the specified index.

final var sb = new StringBuffer("abc");

//sb.codePointBefore(0); // IndexOutOfBoundsException
System.out.printf("%c%n", sb.codePointBefore(1));  // a
System.out.printf("%c%n", sb.codePointBefore(2));  // b
System.out.printf("%c%n", sb.codePointBefore(3));  // c
//sb.codePointBefore(4); // IndexOutOfBoundsException
// An example with a surrogate code point.
final var surrogate = "\uD83D\uDE0A"; // "😊" is one character.
System.out.println(surrogate); // "😊"

final var sb = new StringBuffer(surrogate + "ab");
System.out.println(sb); // "😊ab"

//sb.codePointBefore(0); // IndexOutOfBoundsException

final var ret1 = sb.codePointBefore(1);
System.out.printf("%c%n", ret1); // ? <- Garbled characters.
System.out.println(Integer.toHexString(ret1)); // d83d

final var ret2 = sb.codePointBefore(2);
System.out.printf("%c%n", ret2); // 😊
System.out.println(Integer.toHexString(ret2)); // 1f60a

final var ret3 = sb.codePointBefore(3);
System.out.printf("%c%n", ret3); // a

final var ret4 = sb.codePointBefore(4);
System.out.printf("%c%n", ret4); // b

//sb.codePointBefore(5); // IndexOutOfBoundsException

int codePointCount (int beginIndex, int endIndex)

Returns the number of Unicode code points in the specified text range of this sequence.

final var sb = new StringBuffer("abc");
System.out.println(sb.codePointCount(0, 3));  // 3
System.out.println(sb.codePointCount(1, 2));  // 1
//sb.codePointCount(-1, 3);  // IndexOutOfBoundsException
//sb.codePointCount(0, 4);  // IndexOutOfBoundsException
// An example with a surrogate code point.
final var surrogate = "\uD83D\uDE0A"; // "😊" is one character.
System.out.println(surrogate); // "😊"

final var sb = new StringBuffer(surrogate + "ab");
System.out.println(sb); // "😊ab"

System.out.println(sb.codePointCount(0, 1));  // 1
System.out.println(sb.codePointCount(0, 2));  // 1
System.out.println(sb.codePointCount(0, 3));  // 2
System.out.println(sb.codePointCount(0, 4));  // 3
//sb.codePointCount(0, 5);  // IndexOutOfBoundsException

IntStream codePoints ()

Returns a stream of code point values from this sequence.

final var sb = new StringBuffer("abcd");

sb.codePoints().forEach(cp -> {
    System.out.printf("%x : %c%n", cp, cp);
});

// Result
// ↓
// "61 : a"
// "62 : b"
// "63 : c"
// "64 : d"
// An example with a surrogate code point.
final var surrogate = "\uD83D\uDE0A"; // "😊" is one character.
System.out.println(surrogate); // "😊"

final var sb = new StringBuffer("ab" + surrogate);
System.out.println(sb); // "ab😊"

sb.codePoints().forEach(cp -> {
    System.out.printf("%x : %c%n", cp, cp);
});

// Result
// ↓
// "61 : a"
// "62 : b"
// "1f60a : 😊"

int compareTo (StringBuffer another)

Compares two StringBuffer instances lexicographically.

final var sb1 = new StringBuffer();
final var sb2 = new StringBuffer();

System.out.println(sb1); // ""
System.out.println(sb2); // ""
System.out.println(sb1.compareTo(sb2)); // 0

sb1.append("aaa");
System.out.println(sb1); // "aaa"
System.out.println(sb2); // ""
System.out.println(sb1.compareTo(sb2)); // 3

sb2.append("bbb");
System.out.println(sb1); // "aaa"
System.out.println(sb2); // "bbb"
System.out.println(sb1.compareTo(sb2)); // -1
final var sb1 = new StringBuffer("aaa");
final var sb2 = new StringBuffer("AAA");

System.out.println(sb1); // "aaa"
System.out.println(sb2); // "AAA"
System.out.println(sb1.compareTo(sb2)); // 32

StringBuffer delete (int start, int end)

Removes the characters in a substring of this sequence.

final var s = "abcde";

System.out.println(new StringBuffer(s).delete(0, 0)); // "abcde"
System.out.println(new StringBuffer(s).delete(0, 1)); // "bcde"
System.out.println(new StringBuffer(s).delete(0, 2)); // "cde"
System.out.println(new StringBuffer(s).delete(0, 3)); // "de"
System.out.println(new StringBuffer(s).delete(0, 4)); // "e"
System.out.println(new StringBuffer(s).delete(0, 5)); // ""
final var s = "abcde";

System.out.println(new StringBuffer(s).delete(0, 5)); // ""
System.out.println(new StringBuffer(s).delete(1, 5)); // "a"
System.out.println(new StringBuffer(s).delete(2, 5)); // "ab"
System.out.println(new StringBuffer(s).delete(3, 5)); // "abc"
System.out.println(new StringBuffer(s).delete(4, 5)); // "abcd"
System.out.println(new StringBuffer(s).delete(5, 5)); // "abcde"

StringBuffer deleteCharAt (int index)

Removes the char at the specified position in this sequence.

final var s = "abcde";

System.out.println(new StringBuffer(s).deleteCharAt(0)); // "bcde"
System.out.println(new StringBuffer(s).deleteCharAt(1)); // "acde"
System.out.println(new StringBuffer(s).deleteCharAt(2)); // "abde"
System.out.println(new StringBuffer(s).deleteCharAt(3)); // "abce"
System.out.println(new StringBuffer(s).deleteCharAt(4)); // "abcd"
//new StringBuffer(s).deleteCharAt(5); // StringIndexOutOfBoundsException
// An example with a surrogate code point.
final var surrogate = "\uD83D\uDE0A"; // "😊" is one character.
System.out.println(surrogate); // "😊"

final var s = surrogate + "ab";
System.out.println(s); // "😊ab"

System.out.println(new StringBuffer(s).deleteCharAt(0)); // "?ab" <- Garbled characters.
System.out.println(new StringBuffer(s).deleteCharAt(1)); // "?ab" <- Garbled characters.
System.out.println(new StringBuffer(s).deleteCharAt(2)); // "😊b"
System.out.println(new StringBuffer(s).deleteCharAt(3)); // "😊a"
//new StringBuffer(s).deleteCharAt(4); // StringIndexOutOfBoundsException

final var sb = new StringBuffer(s);
sb.deleteCharAt(0).deleteCharAt(0);
System.out.println(sb); // "ab"

void ensureCapacity (int minimumCapacity)

Ensures that the capacity is at least equal to the specified minimum.

final var sb = new StringBuffer();
System.out.println(sb.capacity()); // 16

sb.ensureCapacity(20);
System.out.println(sb.capacity()); // 34

sb.ensureCapacity(1);
System.out.println(sb.capacity()); // 34

void getChars (int srcBegin, int srcEnd, char[] dst, int dstBegin)

Characters are copied from this sequence into the destination character array dst.

final var sb = new StringBuffer("abcd");

final var chars1 = new char[4];
sb.getChars(0, 0, chars1, 0);
System.out.println(Arrays.toString(chars1)); // [ ,  ,  ,  ]

final var chars2 = new char[4];
sb.getChars(0, 1, chars2, 0);
System.out.println(Arrays.toString(chars2)); // [a,  ,  ,  ]

final var chars3 = new char[4];
sb.getChars(0, 2, chars3, 0);
System.out.println(Arrays.toString(chars3)); // [a, b,  ,  ]

final var chars4 = new char[4];
sb.getChars(0, 3, chars4, 0);
System.out.println(Arrays.toString(chars4)); // [a, b, c,  ]

final var chars5 = new char[4];
sb.getChars(0, 4, chars5, 0);
System.out.println(Arrays.toString(chars5)); // [a, b, c, d]
final var sb = new StringBuffer("abcd");

final var chars1 = new char[4];
sb.getChars(0, 4, chars1, 0);
System.out.println(Arrays.toString(chars1)); // [a, b, c, d]

final var chars2 = new char[4];
sb.getChars(1, 4, chars2, 0);
System.out.println(Arrays.toString(chars2)); // [b, c, d,  ]

final var chars3 = new char[4];
sb.getChars(2, 4, chars3, 0);
System.out.println(Arrays.toString(chars3)); // [c, d,  ,  ]

final var chars4 = new char[4];
sb.getChars(3, 4, chars4, 0);
System.out.println(Arrays.toString(chars4)); // [d,  ,  ,  ]

final var chars5 = new char[4];
sb.getChars(4, 4, chars5, 0);
System.out.println(Arrays.toString(chars5)); // [ ,  ,  ,  ]
final var sb = new StringBuffer("abcd");

final var chars1 = new char[4];
sb.getChars(0, 2, chars1, 0);
System.out.println(Arrays.toString(chars1)); // [a, b,  ,  ]

final var chars2 = new char[4];
sb.getChars(0, 2, chars2, 1);
System.out.println(Arrays.toString(chars2)); // [ , a, b,  ]

final var chars3 = new char[4];
sb.getChars(0, 2, chars3, 2);
System.out.println(Arrays.toString(chars3)); // [ ,  , a, b]

int indexOf (String str)

Returns the index within this string of the first occurrence of the specified substring.

final var sb = new StringBuffer("abcabc");

System.out.println(sb.indexOf("a")); // 0
System.out.println(sb.indexOf("ab")); // 0
System.out.println(sb.indexOf("abc")); // 0
System.out.println(sb.indexOf("bc")); // 1
System.out.println(sb.indexOf("c")); // 2
System.out.println(sb.indexOf("cab")); // 2

System.out.println(sb.indexOf("abcd")); // -1
System.out.println(sb.indexOf("ac")); // -1
System.out.println(sb.indexOf("z")); // -1
// An example with a surrogate code point.
final var surrogate = "\uD83D\uDE0A"; // "😊" is one character.
System.out.println(surrogate); // "😊"

final var sb = new StringBuffer("a" + surrogate + "b");
System.out.println(sb); // "a😊b"

System.out.println(sb.indexOf("a")); // 0
System.out.println(sb.indexOf(surrogate)); // 1
System.out.println(sb.indexOf("b")); // 3

int indexOf (String str, int fromIndex)

Returns the index within this string of the first occurrence of the specified substring, starting at the specified index.

final var sb = new StringBuffer("abcabc");

System.out.println(sb.indexOf("ab", -1)); // 0
System.out.println(sb.indexOf("ab", 0)); // 0
System.out.println(sb.indexOf("ab", 1)); // 3
System.out.println(sb.indexOf("ab", 2)); // 3
System.out.println(sb.indexOf("ab", 3)); // 3
System.out.println(sb.indexOf("ab", 4)); // -1
System.out.println(sb.indexOf("ab", 5)); // -1
System.out.println(sb.indexOf("ab", 6)); // -1

System.out.println(sb.indexOf("bc", 0)); // 1
System.out.println(sb.indexOf("bc", 1)); // 1
System.out.println(sb.indexOf("bc", 2)); // 4
// An example with a surrogate code point.
final var surrogate = "\uD83D\uDE0A"; // "😊" is one character.
System.out.println(surrogate); // "😊"

final var sb = new StringBuffer("a" + surrogate + "b");
System.out.println(sb); // "a😊b"

System.out.println(sb.indexOf("a", 0)); // 0
System.out.println(sb.indexOf("a", 1)); // -1

System.out.println(sb.indexOf(surrogate, 0)); // 1
System.out.println(sb.indexOf(surrogate, 1)); // 1
System.out.println(sb.indexOf(surrogate, 2)); // -1
System.out.println(sb.indexOf(surrogate, 3)); // -1

System.out.println(sb.indexOf("b", 0)); // 3
System.out.println(sb.indexOf("b", 1)); // 3
System.out.println(sb.indexOf("b", 2)); // 3
System.out.println(sb.indexOf("b", 3)); // 3
System.out.println(sb.indexOf("b", 4)); // -1

StringBuffer insert (int offset, boolean b)

Inserts the string representation of the boolean argument into this sequence.

final var s = "ABCDE";

System.out.println(new StringBuffer(s).insert(0, true)); // "trueABCDE"
System.out.println(new StringBuffer(s).insert(1, true)); // "AtrueBCDE"
System.out.println(new StringBuffer(s).insert(2, true)); // "ABtrueCDE"
System.out.println(new StringBuffer(s).insert(3, true)); // "ABCtrueDE"
System.out.println(new StringBuffer(s).insert(4, true)); // "ABCDtrueE"
System.out.println(new StringBuffer(s).insert(5, true)); // "ABCDEtrue"
//new StringBuffer(s).insert(6, true); // StringIndexOutOfBoundsException
final var sb = new StringBuffer("ABC");
sb.insert(0, false).insert(7, true);
System.out.println(sb); // "falseABtrueC"

StringBuffer insert (int offset, char c)

Inserts the string representation of the char argument into this sequence.

final var s = "abcde";

System.out.println(new StringBuffer(s).insert(0, 'Z')); // "Zabcde"
System.out.println(new StringBuffer(s).insert(1, 'Z')); // "aZbcde"
System.out.println(new StringBuffer(s).insert(2, 'Z')); // "abZcde"
System.out.println(new StringBuffer(s).insert(3, 'Z')); // "abcZde"
System.out.println(new StringBuffer(s).insert(4, 'Z')); // "abcdZe"
System.out.println(new StringBuffer(s).insert(5, 'Z')); // "abcdeZ"
//new StringBuffer(s).insert(6, 'Z'); // StringIndexOutOfBoundsException
final var sb = new StringBuffer("abc");
sb.insert(0, 'X').insert(2, 'Y').insert(4, 'Z');
System.out.println(sb); // "XaYbZc"

StringBuffer insert (int offset, char[] str)

Inserts the string representation of the char array argument into this sequence.

final var s = "abcde";
final char[] chars = {'X', 'Y', 'Z'};

System.out.println(new StringBuffer(s).insert(0, chars)); // "XYZabcde"
System.out.println(new StringBuffer(s).insert(1, chars)); // "aXYZbcde"
System.out.println(new StringBuffer(s).insert(2, chars)); // "abXYZcde"
System.out.println(new StringBuffer(s).insert(3, chars)); // "abcXYZde"
System.out.println(new StringBuffer(s).insert(4, chars)); // "abcdXYZe"
System.out.println(new StringBuffer(s).insert(5, chars)); // "abcdeXYZ"
//new StringBuffer(s).insert(6, chars); // StringIndexOutOfBoundsException

StringBuffer insert (int index, char[] str, int offset, int len)

Inserts the string representation of a subarray of the str array argument into this sequence.

final var s = "abcde";
final char[] chars = {'X', 'Y', 'Z'};

System.out.println(new StringBuffer(s).insert(0, chars, 0, 3)); // "XYZabcde"
System.out.println(new StringBuffer(s).insert(1, chars, 0, 3)); // "aXYZbcde"
System.out.println(new StringBuffer(s).insert(2, chars, 0, 3)); // "abXYZcde"
System.out.println(new StringBuffer(s).insert(3, chars, 0, 3)); // "abcXYZde"
System.out.println(new StringBuffer(s).insert(4, chars, 0, 3)); // "abcdXYZe"
System.out.println(new StringBuffer(s).insert(5, chars, 0, 3)); // "abcdeXYZ"
//new StringBuffer(s).insert(6, chars, 0, 3); // StringIndexOutOfBoundsException

System.out.println(new StringBuffer(s).insert(0, chars, 0, 0)); // "abcde"
System.out.println(new StringBuffer(s).insert(0, chars, 0, 1)); // "Xabcde"
System.out.println(new StringBuffer(s).insert(0, chars, 0, 2)); // "XYabcde"
System.out.println(new StringBuffer(s).insert(0, chars, 0, 3)); // "XYZabcde"

System.out.println(new StringBuffer(s).insert(0, chars, 0, 1)); // "Xabcde"
System.out.println(new StringBuffer(s).insert(0, chars, 1, 1)); // "Yabcde"
System.out.println(new StringBuffer(s).insert(0, chars, 2, 1)); // "Zabcde"

StringBuffer insert (int offset, double d)

Inserts the string representation of the double argument into this sequence.

final var s = "abcde";
final var d = 1.23;

System.out.println(new StringBuffer(s).insert(0, d)); // "1.23abcde"
System.out.println(new StringBuffer(s).insert(1, d)); // "a1.23bcde"
System.out.println(new StringBuffer(s).insert(2, d)); // "ab1.23cde"
System.out.println(new StringBuffer(s).insert(3, d)); // "abc1.23de"
System.out.println(new StringBuffer(s).insert(4, d)); // "abcd1.23e"
System.out.println(new StringBuffer(s).insert(5, d)); // "abcde1.23"
//new StringBuffer(s).insert(6, d); // StringIndexOutOfBoundsException
final var sb = new StringBuffer("abc");
sb.insert(0, 1.234).insert(6, 5.678e+20);
System.out.println(sb); // "1.234a5.678E20bc"

StringBuffer insert (int offset, float f)

Inserts the string representation of the float argument into this sequence.

final var s = "abcde";
final var f = 1.23f;

System.out.println(new StringBuffer(s).insert(0, f)); // "1.23abcde"
System.out.println(new StringBuffer(s).insert(1, f)); // "a1.23bcde"
System.out.println(new StringBuffer(s).insert(2, f)); // "ab1.23cde"
System.out.println(new StringBuffer(s).insert(3, f)); // "abc1.23de"
System.out.println(new StringBuffer(s).insert(4, f)); // "abcd1.23e"
System.out.println(new StringBuffer(s).insert(5, f)); // "abcde1.23"
//new StringBuffer(s).insert(6, f); // StringIndexOutOfBoundsException
final var sb = new StringBuffer("abc");
sb.insert(0, 1.234f).insert(6, 5.678e+20f);
System.out.println(sb); // "1.234a5.678E20bc"

StringBuffer insert (int offset, int i)

Inserts the string representation of the second int argument into this sequence.

final var s = "abcde";
final var i = 123;

System.out.println(new StringBuffer(s).insert(0, i)); // "123abcde"
System.out.println(new StringBuffer(s).insert(1, i)); // "a123bcde"
System.out.println(new StringBuffer(s).insert(2, i)); // "ab123cde"
System.out.println(new StringBuffer(s).insert(3, i)); // "abc123de"
System.out.println(new StringBuffer(s).insert(4, i)); // "abcd123e"
System.out.println(new StringBuffer(s).insert(5, i)); // "abcde123"
//new StringBuffer(s).insert(6, i); // StringIndexOutOfBoundsException
final var sb = new StringBuffer("abc");
sb.insert(0, 1234).insert(6, 5678);
System.out.println(sb); // "1234ab5678c"

StringBuffer insert (int offset, long l)

Inserts the string representation of the long argument into this sequence.

final var s = "abcde";
final var l = 123L;

System.out.println(new StringBuffer(s).insert(0, l)); // "123abcde"
System.out.println(new StringBuffer(s).insert(1, l)); // "a123bcde"
System.out.println(new StringBuffer(s).insert(2, l)); // "ab123cde"
System.out.println(new StringBuffer(s).insert(3, l)); // "abc123de"
System.out.println(new StringBuffer(s).insert(4, l)); // "abcd123e"
System.out.println(new StringBuffer(s).insert(5, l)); // "abcde123"
//new StringBuffer(s).insert(6, l); // StringIndexOutOfBoundsException
final var sb = new StringBuffer("abc");
sb.insert(0, 1234L).insert(6, 1234567890123456789L);
System.out.println(sb); // "1234ab1234567890123456789c"

StringBuffer insert (int dstOffset, CharSequence s)

Inserts the specified CharSequence into this sequence.

final var s = "abcde";
final CharSequence seq = new StringBuffer("XYZ");

System.out.println(new StringBuffer(s).insert(0, seq)); // "XYZabcde"
System.out.println(new StringBuffer(s).insert(1, seq)); // "aXYZbcde"
System.out.println(new StringBuffer(s).insert(2, seq)); // "abXYZcde"
System.out.println(new StringBuffer(s).insert(3, seq)); // "abcXYZde"
System.out.println(new StringBuffer(s).insert(4, seq)); // "abcdXYZe"
System.out.println(new StringBuffer(s).insert(5, seq)); // "abcdeXYZ"
//new StringBuffer(s).insert(6, seq); // StringIndexOutOfBoundsException

StringBuffer insert (int dstOffset, CharSequence s, int start, int end)

Inserts a subsequence of the specified CharSequence into this sequence.

final var s = "abcde";
final CharSequence seq = new StringBuffer("XYZ");

System.out.println(new StringBuffer(s).insert(0, seq, 0, 3)); // "XYZabcde"
System.out.println(new StringBuffer(s).insert(1, seq, 0, 3)); // "aXYZbcde"
System.out.println(new StringBuffer(s).insert(2, seq, 0, 3)); // "abXYZcde"
System.out.println(new StringBuffer(s).insert(3, seq, 0, 3)); // "abcXYZde"
System.out.println(new StringBuffer(s).insert(4, seq, 0, 3)); // "abcdXYZe"
System.out.println(new StringBuffer(s).insert(5, seq, 0, 3)); // "abcdeXYZ"
//new StringBuffer(s).insert(6, seq, 0, 3); // IndexOutOfBoundsException

System.out.println(new StringBuffer(s).insert(0, seq, 0, 0)); // "abcde"
System.out.println(new StringBuffer(s).insert(0, seq, 0, 1)); // "Xabcde"
System.out.println(new StringBuffer(s).insert(0, seq, 0, 2)); // "XYabcde"
System.out.println(new StringBuffer(s).insert(0, seq, 0, 3)); // "XYZabcde"

System.out.println(new StringBuffer(s).insert(0, seq, 0, 3)); // "XYZabcde"
System.out.println(new StringBuffer(s).insert(0, seq, 1, 3)); // "YZabcde"
System.out.println(new StringBuffer(s).insert(0, seq, 2, 3)); // "Zabcde"
System.out.println(new StringBuffer(s).insert(0, seq, 3, 3)); // "abcde"

StringBuffer insert (int offset, Object obj)

Inserts the string representation of the Object argument into this character sequence.

final var s = "abcde";
final var obj = LocalDate.of(2100, 1, 1);

System.out.println(new StringBuffer(s).insert(0, obj)); // "2100-01-01abcde"
System.out.println(new StringBuffer(s).insert(1, obj)); // "a2100-01-01bcde"
System.out.println(new StringBuffer(s).insert(2, obj)); // "ab2100-01-01cde"
System.out.println(new StringBuffer(s).insert(3, obj)); // "abc2100-01-01de"
System.out.println(new StringBuffer(s).insert(4, obj)); // "abcd2100-01-01e"
System.out.println(new StringBuffer(s).insert(5, obj)); // "abcde2100-01-01"
//new StringBuffer(s).insert(6, obj); // StringIndexOutOfBoundsException
final var sb = new StringBuffer("abc");
final var obj1 = LocalDate.of(2100, 1, 1);
final var obj2 = URI.create("https://example.com/");

sb.insert(0, obj1).insert(12, obj2);
System.out.println(sb); // "2100-01-01abhttps://example.com/c"

StringBuffer insert (int offset, String str)

Inserts the string into this character sequence.

final var s = "abcde";
final var str = "XYZ";

System.out.println(new StringBuffer(s).insert(0, str)); // "XYZabcde"
System.out.println(new StringBuffer(s).insert(1, str)); // "aXYZbcde"
System.out.println(new StringBuffer(s).insert(2, str)); // "abXYZcde"
System.out.println(new StringBuffer(s).insert(3, str)); // "abcXYZde"
System.out.println(new StringBuffer(s).insert(4, str)); // "abcdXYZe"
System.out.println(new StringBuffer(s).insert(5, str)); // "abcdeXYZ"
//new StringBuffer(s).insert(6, str); // StringIndexOutOfBoundsException

int lastIndexOf (String str)

Returns the index within this string of the last occurrence of the specified substring.

final var sb = new StringBuffer("abcabc");

System.out.println(sb.lastIndexOf("a")); // 3
System.out.println(sb.lastIndexOf("ab")); // 3
System.out.println(sb.lastIndexOf("abc")); // 3
System.out.println(sb.lastIndexOf("bc")); // 4
System.out.println(sb.lastIndexOf("c")); // 5
System.out.println(sb.lastIndexOf("cab")); // 2

System.out.println(sb.lastIndexOf("abcd")); // -1
System.out.println(sb.lastIndexOf("ac")); // -1
System.out.println(sb.lastIndexOf("z")); // -1
// An example with a surrogate code point.
final var surrogate = "\uD83D\uDE0A"; // "😊" is one character.
System.out.println(surrogate); // "😊"

final var sb = new StringBuffer("a" + surrogate + "b");
System.out.println(sb); // "a😊b"

System.out.println(sb.lastIndexOf("a")); // 0
System.out.println(sb.lastIndexOf(surrogate)); // 1
System.out.println(sb.lastIndexOf("b")); // 3

int lastIndexOf (String str, int fromIndex)

Returns the index within this string of the last occurrence of the specified substring, searching backward starting at the specified index.

final var sb = new StringBuffer("abcabc");

System.out.println(sb.lastIndexOf("ab", 5)); // 3
System.out.println(sb.lastIndexOf("ab", 4)); // 3
System.out.println(sb.lastIndexOf("ab", 3)); // 3
System.out.println(sb.lastIndexOf("ab", 2)); // 0
System.out.println(sb.lastIndexOf("ab", 1)); // 0
System.out.println(sb.lastIndexOf("ab", 0)); // 0

System.out.println(sb.lastIndexOf("bc", 5)); // 4
System.out.println(sb.lastIndexOf("bc", 4)); // 4
System.out.println(sb.lastIndexOf("bc", 3)); // 1
System.out.println(sb.lastIndexOf("bc", 2)); // 1
System.out.println(sb.lastIndexOf("bc", 1)); // 1
System.out.println(sb.lastIndexOf("bc", 0)); // -1

System.out.println(sb.lastIndexOf("ca", 5)); // 2
System.out.println(sb.lastIndexOf("ca", 4)); // 2
System.out.println(sb.lastIndexOf("ca", 3)); // 2
System.out.println(sb.lastIndexOf("ca", 2)); // 2
System.out.println(sb.lastIndexOf("ca", 1)); // -1
System.out.println(sb.lastIndexOf("ca", 0)); // -1
// An example with a surrogate code point.
final var surrogate = "\uD83D\uDE0A"; // "😊" is one character.
System.out.println(surrogate); // "😊"

final var sb = new StringBuffer("a" + surrogate + "b");
System.out.println(sb); // "a😊b"

System.out.println(sb.lastIndexOf("a", 3)); // 0
System.out.println(sb.lastIndexOf("a", 2)); // 0
System.out.println(sb.lastIndexOf("a", 1)); // 0
System.out.println(sb.lastIndexOf("a", 0)); // 0

System.out.println(sb.lastIndexOf(surrogate, 3)); // 1
System.out.println(sb.lastIndexOf(surrogate, 2)); // 1
System.out.println(sb.lastIndexOf(surrogate, 1)); // 1
System.out.println(sb.lastIndexOf(surrogate, 0)); // -1

System.out.println(sb.lastIndexOf("b", 3)); // 3
System.out.println(sb.lastIndexOf("b", 2)); // -1
System.out.println(sb.lastIndexOf("b", 1)); // -1
System.out.println(sb.lastIndexOf("b", 0)); // -1

int length ()

Returns the length (character count).

final var sb = new StringBuffer();

System.out.println(sb); // ""
System.out.println(sb.length()); // 0

sb.append("abcd");
System.out.println(sb); // "abcd"
System.out.println(sb.length()); // 4

sb.append("0123456789");
System.out.println(sb); // "abcd0123456789"
System.out.println(sb.length()); // 14

int offsetByCodePoints (int index, int codePointOffset)

Returns the index within this sequence that is offset from the given index by codePointOffset code points.

final var sb = new StringBuffer("abc");

//sb.offsetByCodePoints(0, -1); // IndexOutOfBoundsException
System.out.println(sb.offsetByCodePoints(0, 0)); // 0
System.out.println(sb.offsetByCodePoints(0, 1)); // 1
System.out.println(sb.offsetByCodePoints(0, 2)); // 2
System.out.println(sb.offsetByCodePoints(0, 3)); // 3

System.out.println(sb.offsetByCodePoints(1, 0)); // 1
System.out.println(sb.offsetByCodePoints(1, 1)); // 2
System.out.println(sb.offsetByCodePoints(1, 2)); // 3
// An example with a surrogate code point.
final var surrogate = "\uD83D\uDE0A"; // "😊" is one character.
System.out.println(surrogate); // "😊"

final var sb = new StringBuffer("a" + surrogate + "b");
System.out.println(sb); // "a😊b"

System.out.println(sb.offsetByCodePoints(0, 0)); // 0
System.out.println(sb.offsetByCodePoints(0, 1)); // 1
System.out.println(sb.offsetByCodePoints(0, 2)); // 3
System.out.println(sb.offsetByCodePoints(0, 3)); // 4

StringBuffer repeat (int codePoint, int count)

Repeats count copies of the string representation of the codePoint argument to this sequence.

final var codePoint = Character.toCodePoint('\uD83D', '\uDE0A');
System.out.printf("%c%n", codePoint); // 😊

final var sb = new StringBuffer();
System.out.println(sb.repeat(codePoint, 3)); // "😊😊😊"

StringBuffer repeat (CharSequence cs, int count)

Appends count copies of the specified CharSequence cs to this sequence.

final var cs = "Abc";
final var sb = new StringBuffer();
System.out.println(sb.repeat(cs, 3)); // "AbcAbcAbc"

StringBuffer replace (int start, int end, String str)

Replaces the characters in a substring of this sequence with characters in the specified String.

final var s = "abcd";

System.out.println(new StringBuffer(s).replace(0, 0, "X")); // "Xabcd"
System.out.println(new StringBuffer(s).replace(0, 1, "X")); // "Xbcd"
System.out.println(new StringBuffer(s).replace(0, 2, "X")); // "Xcd"
System.out.println(new StringBuffer(s).replace(0, 3, "X")); // "Xd"
System.out.println(new StringBuffer(s).replace(0, 4, "X")); // "X"

System.out.println(new StringBuffer(s).replace(1, 4, "X")); // "aX"
System.out.println(new StringBuffer(s).replace(2, 4, "X")); // "abX"
System.out.println(new StringBuffer(s).replace(3, 4, "X")); // "abcX"
System.out.println(new StringBuffer(s).replace(4, 4, "X")); // "abcdX"
final var sb = new StringBuffer("abcdef");

sb.replace(0, 2, "AB").replace(3, 6, "DEF");
System.out.println(sb); // "ABcDEF"

StringBuffer reverse ()

Causes this character sequence to be replaced by the reverse of the sequence.

final var sb = new StringBuffer("abcdef");
System.out.println(sb.reverse()); // "fedcba"
// An example with a surrogate code point.
final var surrogate = "\uD83D\uDE0A"; // "😊" is one character.
System.out.println(surrogate); // "😊"

final var sb = new StringBuffer("a" + surrogate + "b");
System.out.println(sb); // "a😊b"
System.out.println(sb.reverse()); // "b😊a"

void setCharAt (int index, char ch)

The character at the specified index is set to ch.

final var sb = new StringBuffer("abcd");

sb.setCharAt(0, 'A');
System.out.println(sb); // "Abcd"

sb.setCharAt(1, 'B');
System.out.println(sb); // "ABcd"

sb.setCharAt(2, 'C');
System.out.println(sb); // "ABCd"

sb.setCharAt(3, 'D');
System.out.println(sb); // "ABCD"

//sb.setCharAt(4, 'X'); // IndexOutOfBoundsException
//sb.setCharAt(-1, 'Y'); // IndexOutOfBoundsException

void setLength (int newLength)

Sets the length of the character sequence.

final var sb = new StringBuffer("abcd");

System.out.println(sb); // "abcd"
System.out.println(sb.length()); // 4

sb.setLength(2);
System.out.println(sb); // "ab"
System.out.println(sb.length()); // 2
final var sb = new StringBuffer("xyz");

System.out.println(sb); // "xyz"
System.out.println(sb.length()); // 3

sb.setLength(5);
System.out.println(sb); // "xyz\0\0"
System.out.println(sb.length()); // 5

// [120, 121, 122, 0, 0]
System.out.println(Arrays.toString(sb.codePoints().toArray()));

CharSequence subSequence (int start, int end)

Returns a new character sequence that is a subsequence of this sequence.

final var sb = new StringBuffer("abcd");

System.out.println(sb.subSequence(0, 0)); // ""
System.out.println(sb.subSequence(0, 1)); // "a"
System.out.println(sb.subSequence(0, 2)); // "ab"
System.out.println(sb.subSequence(0, 3)); // "abc"
System.out.println(sb.subSequence(0, 4)); // "abcd"
//sb.subSequence(0, 5); // IndexOutOfBoundsException

System.out.println(sb.subSequence(0, 4)); // "abcd"
System.out.println(sb.subSequence(1, 4)); // "bcd"
System.out.println(sb.subSequence(2, 4)); // "cd"
System.out.println(sb.subSequence(3, 4)); // "d"
System.out.println(sb.subSequence(4, 4)); // ""

String substring (int start)

Returns a new String that contains a subsequence of characters currently contained in this character sequence.

final var sb = new StringBuffer("abcd");

System.out.println(sb.substring(0)); // "abcd"
System.out.println(sb.substring(1)); // "bcd"
System.out.println(sb.substring(2)); // "cd"
System.out.println(sb.substring(3)); // "d"
System.out.println(sb.substring(4)); // ""
//sb.substring(5); // StringIndexOutOfBoundsException

String substring (int start, int end)

Returns a new String that contains a subsequence of characters currently contained in this sequence.

final var sb = new StringBuffer("abcd");

System.out.println(sb.substring(0, 0)); // ""
System.out.println(sb.substring(0, 1)); // "a"
System.out.println(sb.substring(0, 2)); // "ab"
System.out.println(sb.substring(0, 3)); // "abc"
System.out.println(sb.substring(0, 4)); // "abcd"
//sb.substring(0, 5); // StringIndexOutOfBoundsException

System.out.println(sb.substring(0, 4)); // "abcd"
System.out.println(sb.substring(1, 4)); // "bcd"
System.out.println(sb.substring(2, 4)); // "cd"
System.out.println(sb.substring(3, 4)); // "d"
System.out.println(sb.substring(4, 4)); // ""

String toString ()

Returns a string representing the data in this sequence.

final var sb = new StringBuffer("abc");

final String str1 = sb.toString();
System.out.println(str1); // "abc"

sb.append("XYZ");

final String str2 = sb.toString();
System.out.println(str2); // "abcXYZ"

void trimToSize ()

Attempts to reduce storage used for the character sequence.

final var sb = new StringBuffer("abc");

System.out.println(sb); // "abc"
System.out.println(sb.length()); // 3
System.out.println(sb.capacity()); // 19

sb.trimToSize();

System.out.println(sb); // "abc"
System.out.println(sb.length()); // 3
System.out.println(sb.capacity()); // 3
final var sb = new StringBuffer(" abc ");

System.out.println(sb); // " abc "
System.out.println(sb.length()); // 5
System.out.println(sb.capacity()); // 21

sb.trimToSize();

System.out.println(sb); // " abc "
System.out.println(sb.length()); // 5
System.out.println(sb.capacity()); // 5

Methods declared in CharSequence

chars, codePoints, isEmpty

Please see the link below.


Related posts

To top of page