Java : StringBuffer - API使用例
StringBuffer (Java SE 19 & JDK 19) の使用例まとめです。
だいたいのメソッドを網羅済みです。
API仕様のおともにどうぞ。
概要
StringBuffer はスレッドセーフなクラスです。
同期が必要ない場合は、代わりに StringBuilder を使用してください。
単一のスレッドからのみ使用する場合、StringBuilder は StringBuffer より高速です。
StringBuilder と StringBuffer の違いは、下記の記事でも解説しています。
合わせてご確認いただけたら幸いです。
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"
コンストラクタ
StringBuffer ()
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)
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)
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)
final var sb = new StringBuffer("abc");
System.out.println(sb); // "abc"
sb.append("xyz");
System.out.println(sb); // "abcxyz"
メソッド
StringBuffer append (boolean b)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
final var obj1 = LocalDate.of(2021, 4, 16);
final var obj2 = new URL("https://example.com/");
final var sb = new StringBuffer();
System.out.println(sb); // ""
sb.append(obj1);
System.out.println(sb); // "2021-04-16"
sb.append(" : ").append(obj2);
System.out.println(sb); // "2021-04-16 : https://example.com/"
StringBuffer append (String str)
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)
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)
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"
// サロゲート・コード・ポイントの例
final var surrogate = "\uD867\uDE3D"; // "𩸽"という1文字
System.out.println(surrogate); // "𩸽"
final var cp = surrogate.codePointAt(0);
System.out.println(Integer.toHexString(cp)); // 29e3d
final var sb = new StringBuffer();
System.out.println(sb); // ""
sb.appendCodePoint(cp);
System.out.println(sb); // "𩸽"
int 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)
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
// サロゲート・コード・ポイントの例
final var surrogate = "\uD867\uDE3D"; // "𩸽"という1文字
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); // ? <- 文字化け
System.out.println(Integer.toHexString(ret1)); // d867 <- サロゲート値
final var ret2 = sb.charAt(1);
System.out.println(ret2); // ? <- 文字化け
System.out.println(Integer.toHexString(ret2)); // de3d <- サロゲート値
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 ()
final var sb = new StringBuffer("abcd");
sb.chars().forEach(c -> {
System.out.println("%x : %c".formatted(c, c));
});
// 結果
// ↓
// "61 : a"
// "62 : b"
// "63 : c"
// "64 : d"
// サロゲート・コード・ポイントの例です。
final var surrogate = "\uD867\uDE3D"; // "𩸽"という1文字
System.out.println(surrogate); // "𩸽"
final var sb = new StringBuffer("ab" + surrogate);
System.out.println(sb); // "ab𩸽"
sb.chars().forEach(c -> {
System.out.println("%x : %c".formatted(c, c));
});
// 結果
// ↓
// "61 : a"
// "62 : b"
// "d867 : ?" <- 文字化け
// "de3d : ?" <- 文字化け
int codePointAt (int index)
final var sb = new StringBuffer("abc");
//sb.codePointAt(-1); // IndexOutOfBoundsException
System.out.println("%c".formatted(sb.codePointAt(0))); // a
System.out.println("%c".formatted(sb.codePointAt(1))); // b
System.out.println("%c".formatted(sb.codePointAt(2))); // c
//sb.codePointAt(3); // IndexOutOfBoundsException
final var sb = new StringBuffer("あいうえお");
System.out.println("%c".formatted(sb.codePointAt(1))); // い
System.out.println("%c".formatted(sb.codePointAt(4))); // お
// サロゲート・コード・ポイントの例
final var surrogate = "\uD867\uDE3D"; // "𩸽"という1文字
System.out.println(surrogate); // "𩸽"
final var sb = new StringBuffer(surrogate + "ab");
System.out.println(sb); // "𩸽ab"
final var ret1 = sb.codePointAt(0);
System.out.println("%c".formatted(ret1)); // 𩸽
System.out.println(Integer.toHexString(ret1)); // 29e3d
final var ret2 = sb.codePointAt(1);
System.out.println("%c".formatted(ret2)); // ? <- 文字化け
System.out.println(Integer.toHexString(ret2)); // de3d <- サロゲート値
final var ret3 = sb.codePointAt(2);
System.out.println("%c".formatted(ret3)); // a
final var ret4 = sb.codePointAt(3);
System.out.println("%c".formatted(ret4)); // b
//sb.codePointAt(4); // IndexOutOfBoundsException
int codePointBefore (int index)
final var sb = new StringBuffer("abc");
//sb.codePointBefore(0); // IndexOutOfBoundsException
System.out.println("%c".formatted(sb.codePointBefore(1))); // a
System.out.println("%c".formatted(sb.codePointBefore(2))); // b
System.out.println("%c".formatted(sb.codePointBefore(3))); // c
//sb.codePointBefore(4); // IndexOutOfBoundsException
final var sb = new StringBuffer("あいう");
System.out.println("%c".formatted(sb.codePointBefore(1))); // あ
System.out.println("%c".formatted(sb.codePointBefore(2))); // い
System.out.println("%c".formatted(sb.codePointBefore(3))); // う
// サロゲート・コード・ポイントの例
final var surrogate = "\uD867\uDE3D"; // "𩸽"という1文字
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.println("%c".formatted(ret1)); // ? <- 文字化け
System.out.println(Integer.toHexString(ret1)); // d867
final var ret2 = sb.codePointBefore(2);
System.out.println("%c".formatted(ret2)); // 𩸽
System.out.println(Integer.toHexString(ret2)); // 29e3d
final var ret3 = sb.codePointBefore(3);
System.out.println("%c".formatted(ret3)); // a
final var ret4 = sb.codePointBefore(4);
System.out.println("%c".formatted(ret4)); // b
//sb.codePointBefore(5); // IndexOutOfBoundsException
int codePointCount (int beginIndex, int endIndex)
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
final var sb = new StringBuffer("かきく");
System.out.println(sb.codePointCount(0, 3)); // 3
System.out.println(sb.codePointCount(1, 2)); // 1
// サロゲート・コード・ポイントの例
final var surrogate = "\uD867\uDE3D"; // "𩸽"という1文字
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 ()
final var sb = new StringBuffer("abcd");
sb.codePoints().forEach(cp -> {
System.out.println("%x : %c".formatted(cp, cp));
});
// 結果
// ↓
// "61 : a"
// "62 : b"
// "63 : c"
// "64 : d"
// サロゲート・コード・ポイントの例です。
final var surrogate = "\uD867\uDE3D"; // "𩸽"という1文字
System.out.println(surrogate); // "𩸽"
final var sb = new StringBuffer("ab" + surrogate);
System.out.println(sb); // "ab𩸽"
sb.codePoints().forEach(cp -> {
System.out.println("%x : %c".formatted(cp, cp));
});
// 結果
// ↓
// "61 : a"
// "62 : b"
// "29e3d : 𩸽"
int compareTo (StringBuffer another)
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)
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)
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
// サロゲート・コード・ポイントの例
final var surrogate = "\uD867\uDE3D"; // "𩸽"という1文字
System.out.println(surrogate); // "𩸽"
final var s = surrogate + "ab";
System.out.println(s); // "𩸽ab"
System.out.println(new StringBuffer(s).deleteCharAt(0)); // "?ab" <- 文字化け
System.out.println(new StringBuffer(s).deleteCharAt(1)); // "?ab" <- 文字化け
System.out.println(new StringBuffer(s).deleteCharAt(2)); // "𩸽b"
System.out.println(new StringBuffer(s).deleteCharAt(3)); // "𩸽a"
//new StringBuffer(s).deleteCharAt(4); // StringIndexOutOfBoundsException
// 2文字分を消します。
final var sb = new StringBuffer(s);
sb.deleteCharAt(0).deleteCharAt(0);
System.out.println(sb); // "ab"
void ensureCapacity (int minimumCapacity)
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)
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)
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
// サロゲート・コード・ポイントの例
final var surrogate = "\uD867\uDE3D"; // "𩸽"という1文字
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)
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
// サロゲート・コード・ポイントの例
final var surrogate = "\uD867\uDE3D"; // "𩸽"という1文字
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)
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)
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)
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
final var sb = new StringBuffer("abc");
final char[] chars1 = {'X', 'Y', 'Z'};
final char[] chars2 = {'あ', 'い', 'う'};
sb.insert(0, chars1).insert(4, chars2);
System.out.println(sb); // "XYZaあいうbc"
StringBuffer insert (int index, char[] str, int offset, int len)
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"
final var sb = new StringBuffer("abc");
final char[] chars1 = {'X', 'Y', 'Z'};
final char[] chars2 = {'あ', 'い', 'う'};
sb.insert(0, chars1, 0, 2).insert(4, chars2, 1, 2);
System.out.println(sb); // "XYabいうc"
StringBuffer insert (int offset, double d)
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)
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)
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)
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)
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
final var sb = new StringBuffer("abc");
final CharSequence seq1 = new StringBuffer("XYZ");
final CharSequence seq2 = new StringBuffer("あいう");
sb.insert(0, seq1).insert(4, seq2);
System.out.println(sb); // "XYZaあいうbc"
StringBuffer insert (int dstOffset, CharSequence s, int start, int end)
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); // StringIndexOutOfBoundsException
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"
final var sb = new StringBuffer("abc");
final CharSequence seq1 = new StringBuffer("XYZ");
final CharSequence seq2 = new StringBuffer("あいう");
sb.insert(0, seq1, 0, 2).insert(4, seq2, 1, 2);
System.out.println(sb); // "XYabいc"
StringBuffer insert (int offset, Object obj)
final var s = "abcde";
final var obj = LocalDate.of(2021, 1, 1);
System.out.println(new StringBuffer(s).insert(0, obj)); // "2021-01-01abcde"
System.out.println(new StringBuffer(s).insert(1, obj)); // "a2021-01-01bcde"
System.out.println(new StringBuffer(s).insert(2, obj)); // "ab2021-01-01cde"
System.out.println(new StringBuffer(s).insert(3, obj)); // "abc2021-01-01de"
System.out.println(new StringBuffer(s).insert(4, obj)); // "abcd2021-01-01e"
System.out.println(new StringBuffer(s).insert(5, obj)); // "abcde2021-01-01"
//new StringBuffer(s).insert(6, obj); // StringIndexOutOfBoundsException
final var sb = new StringBuffer("abc");
final var obj1 = LocalDate.of(2021, 1, 1);
final var obj2 = new URL("https://example.com/");
sb.insert(0, obj1).insert(12, obj2);
System.out.println(sb); // "2021-01-01abhttps://example.com/c"
StringBuffer insert (int offset, String str)
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
final var sb = new StringBuffer("abc");
final var str1 = "XYZ";
final var str2 = "あいう";
sb.insert(0, str1).insert(4, str2);
System.out.println(sb); // "XYZaあいうbc"
int lastIndexOf (String str)
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
// サロゲート・コード・ポイントの例
final var surrogate = "\uD867\uDE3D"; // "𩸽"という1文字
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)
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
// サロゲート・コード・ポイントの例
final var surrogate = "\uD867\uDE3D"; // "𩸽"という1文字
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 ()
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)
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
// サロゲート・コード・ポイントの例
final var surrogate = "\uD867\uDE3D"; // "𩸽"という1文字
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 replace (int start, int end, String str)
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 ()
final var sb = new StringBuffer("abcdef");
System.out.println(sb.reverse()); // "fedcba"
final var sb = new StringBuffer("あいうえお");
System.out.println(sb.reverse()); // "おえういあ"
// サロゲート・コード・ポイントの例
final var surrogate = "\uD867\uDE3D"; // "𩸽"という1文字
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)
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'); // StringIndexOutOfBoundsException
//sb.setCharAt(-1, 'Y'); // StringIndexOutOfBoundsException
void setLength (int newLength)
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)
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)
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); // IndexOutOfBoundsException
String substring (int start, int end)
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); // IndexOutOfBoundsException
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 ()
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 ()
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
CharSequenceで宣言されたメソッド
chars, codePoints, isEmpty
「Java API 使用例 : CharSequence」をご参照ください。