Java : StringBuilder - API使用例
StringBuilder (Java SE 22 & JDK 22) の使い方まとめです。
ほとんどのメソッドにサンプルコードがあります。
API仕様書のおともにどうぞ。
概要
StringBuilderでは、同期化は保証されません。
複数スレッドからのアクセスがある場合は、代わりに StringBuffer を使用してください。
単一のスレッドからのみ使用する場合、StringBuilder は StringBuffer より高速です。
StringBuilder と StringBuffer の違いは、下記の記事でも解説しています。
合わせてご確認いただけたら幸いです。
final var sb = new StringBuilder();
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"
コンストラクタ
StringBuilder ()
final var sb = new StringBuilder();
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
StringBuilder (int capacity)
final var sb = new StringBuilder(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
StringBuilder (CharSequence seq)
final CharSequence seq = new StringBuilder("abc");
System.out.println(seq); // "abc"
final var sb = new StringBuilder(seq);
System.out.println(sb); // "abc"
sb.append("xyz");
System.out.println(sb); // "abcxyz"
System.out.println(seq); // "abc"
StringBuilder (String str)
final var sb = new StringBuilder("abc");
System.out.println(sb); // "abc"
sb.append("xyz");
System.out.println(sb); // "abcxyz"
メソッド
StringBuilder append (boolean b)
final var sb = new StringBuilder();
System.out.println(sb); // ""
sb.append(true);
System.out.println(sb); // "true"
sb.append(true).append(false);
System.out.println(sb); // "truetruefalse"
StringBuilder append (char c)
final var sb = new StringBuilder();
System.out.println(sb); // ""
sb.append('a');
System.out.println(sb); // "a"
sb.append('b').append('c');
System.out.println(sb); // "abc"
StringBuilder append (char[] str)
final var sb = new StringBuilder();
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-!"
StringBuilder append (char[] str, int offset, int len)
final var sb = new StringBuilder();
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"
StringBuilder append (double d)
final var sb = new StringBuilder();
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"
StringBuilder append (float f)
final var sb = new StringBuilder();
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"
StringBuilder append (int i)
final var sb = new StringBuilder();
System.out.println(sb); // ""
sb.append(-1234);
System.out.println(sb); // "-1234"
sb.append(" : ").append(5678);
System.out.println(sb); // "-1234 : 5678"
StringBuilder append (long lng)
final var sb = new StringBuilder();
System.out.println(sb); // ""
sb.append(-1234L);
System.out.println(sb); // "-1234"
sb.append(" : ").append(1234567890123456789L);
System.out.println(sb); // "-1234 : 1234567890123456789"
StringBuilder append (CharSequence s)
final CharSequence cs1 = new StringBuilder("abc");
final CharSequence cs2 = new StringBuilder("XYZ");
final CharSequence cs3 = new StringBuilder("-!");
final var sb = new StringBuilder();
System.out.println(sb); // ""
sb.append(cs1);
System.out.println(sb); // "abc"
sb.append(cs2).append(cs3);
System.out.println(sb); // "abcXYZ-!"
StringBuilder append (CharSequence s, int start, int end)
final CharSequence cs = new StringBuilder("abXYZ");
final var sb = new StringBuilder();
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"
StringBuilder append (Object obj)
final var obj1 = LocalDate.of(2100, 4, 16);
final var obj2 = URI.create("https://example.com/");
final var sb = new StringBuilder();
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/"
StringBuilder append (String str)
final var sb = new StringBuilder();
System.out.println(sb); // ""
sb.append("abc");
System.out.println(sb); // "abc"
sb.append("XYZ").append("-!");
System.out.println(sb); // "abcXYZ-!"
StringBuilder append (StringBuffer sb)
final var buffer1 = new StringBuffer("abc");
final var buffer2 = new StringBuffer("XYZ");
final var buffer3 = new StringBuffer("-!");
final var sb = new StringBuilder();
System.out.println(sb); // ""
sb.append(buffer1);
System.out.println(sb); // "abc"
sb.append(buffer2).append(buffer3);
System.out.println(sb); // "abcXYZ-!"
StringBuilder 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 StringBuilder();
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 = "\uD83D\uDE0A"; // "😊"という1文字
System.out.println(surrogate); // "😊"
final var cp = surrogate.codePointAt(0);
System.out.println(Integer.toHexString(cp)); // 1f60a
final var sb = new StringBuilder();
System.out.println(sb); // ""
sb.appendCodePoint(cp);
System.out.println(sb); // "😊"
int capacity ()
final var sb = new StringBuilder(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 StringBuilder("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 = "\uD83D\uDE0A"; // "😊"という1文字
System.out.println(surrogate); // "😊"
final var sb = new StringBuilder(surrogate + "ab");
System.out.println(sb); // "😊ab"
final var ret1 = sb.charAt(0);
System.out.println(ret1); // ? <- 文字化け
System.out.println(Integer.toHexString(ret1)); // d83d
final var ret2 = sb.charAt(1);
System.out.println(ret2); // ? <- 文字化け
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 ()
final var sb = new StringBuilder("abcd");
sb.chars().forEach(c -> {
System.out.printf("%x : %c%n", c, c);
});
// 結果
// ↓
// "61 : a"
// "62 : b"
// "63 : c"
// "64 : d"
// サロゲート・コード・ポイントの例です。
final var surrogate = "\uD83D\uDE0A"; // "😊"という1文字
System.out.println(surrogate); // "😊"
final var sb = new StringBuilder("ab" + surrogate);
System.out.println(sb); // "ab😊"
sb.chars().forEach(c -> {
System.out.printf("%x : %c%n", c, c);
});
// 結果
// ↓
// "61 : a"
// "62 : b"
// "d83d : ?" <- 文字化け
// "de0a : ?" <- 文字化け
int codePointAt (int index)
final var sb = new StringBuilder("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
// サロゲート・コード・ポイントの例です。
final var surrogate = "\uD83D\uDE0A"; // "😊"という1文字
System.out.println(surrogate); // "😊"
final var sb = new StringBuilder(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); // ? <- 文字化け
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)
final var sb = new StringBuilder("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
// サロゲート・コード・ポイントの例です。
final var surrogate = "\uD83D\uDE0A"; // "😊"という1文字
System.out.println(surrogate); // "😊"
final var sb = new StringBuilder(surrogate + "ab");
System.out.println(sb); // "😊ab"
//sb.codePointBefore(0); // IndexOutOfBoundsException
final var ret1 = sb.codePointBefore(1);
System.out.printf("%c%n", ret1); // ? <- 文字化け
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)
final var sb = new StringBuilder("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 surrogate = "\uD83D\uDE0A"; // "😊"という1文字
System.out.println(surrogate); // "😊"
final var sb = new StringBuilder(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 StringBuilder("abcd");
sb.codePoints().forEach(cp -> {
System.out.printf("%x : %c%n", cp, cp);
});
// 結果
// ↓
// "61 : a"
// "62 : b"
// "63 : c"
// "64 : d"
// サロゲート・コード・ポイントの例です。
final var surrogate = "\uD83D\uDE0A"; // "😊"という1文字
System.out.println(surrogate); // "😊"
final var sb = new StringBuilder("ab" + surrogate);
System.out.println(sb); // "ab😊"
sb.codePoints().forEach(cp -> {
System.out.printf("%x : %c%n", cp, cp);
});
// 結果
// ↓
// "61 : a"
// "62 : b"
// "1f60a : 😊"
int compareTo (StringBuilder another)
final var sb1 = new StringBuilder();
final var sb2 = new StringBuilder();
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 StringBuilder("aaa");
final var sb2 = new StringBuilder("AAA");
System.out.println(sb1); // "aaa"
System.out.println(sb2); // "AAA"
System.out.println(sb1.compareTo(sb2)); // 32
StringBuilder delete (int start, int end)
final var s = "abcde";
System.out.println(new StringBuilder(s).delete(0, 0)); // "abcde"
System.out.println(new StringBuilder(s).delete(0, 1)); // "bcde"
System.out.println(new StringBuilder(s).delete(0, 2)); // "cde"
System.out.println(new StringBuilder(s).delete(0, 3)); // "de"
System.out.println(new StringBuilder(s).delete(0, 4)); // "e"
System.out.println(new StringBuilder(s).delete(0, 5)); // ""
final var s = "abcde";
System.out.println(new StringBuilder(s).delete(0, 5)); // ""
System.out.println(new StringBuilder(s).delete(1, 5)); // "a"
System.out.println(new StringBuilder(s).delete(2, 5)); // "ab"
System.out.println(new StringBuilder(s).delete(3, 5)); // "abc"
System.out.println(new StringBuilder(s).delete(4, 5)); // "abcd"
System.out.println(new StringBuilder(s).delete(5, 5)); // "abcde"
StringBuilder deleteCharAt (int index)
final var s = "abcde";
System.out.println(new StringBuilder(s).deleteCharAt(0)); // "bcde"
System.out.println(new StringBuilder(s).deleteCharAt(1)); // "acde"
System.out.println(new StringBuilder(s).deleteCharAt(2)); // "abde"
System.out.println(new StringBuilder(s).deleteCharAt(3)); // "abce"
System.out.println(new StringBuilder(s).deleteCharAt(4)); // "abcd"
//new StringBuilder(s).deleteCharAt(5); // StringIndexOutOfBoundsException
// サロゲート・コード・ポイントの例です。
final var surrogate = "\uD83D\uDE0A"; // "😊"という1文字
System.out.println(surrogate); // "😊"
final var s = surrogate + "ab";
System.out.println(s); // "😊ab"
System.out.println(new StringBuilder(s).deleteCharAt(0)); // "?ab" <- 文字化け
System.out.println(new StringBuilder(s).deleteCharAt(1)); // "?ab" <- 文字化け
System.out.println(new StringBuilder(s).deleteCharAt(2)); // "😊b"
System.out.println(new StringBuilder(s).deleteCharAt(3)); // "😊a"
//new StringBuilder(s).deleteCharAt(4); // StringIndexOutOfBoundsException
final var sb = new StringBuilder(s);
sb.deleteCharAt(0).deleteCharAt(0);
System.out.println(sb); // "ab"
void ensureCapacity (int minimumCapacity)
final var sb = new StringBuilder();
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 StringBuilder("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 StringBuilder("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 StringBuilder("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 StringBuilder("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 = "\uD83D\uDE0A"; // "😊"という1文字
System.out.println(surrogate); // "😊"
final var sb = new StringBuilder("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 StringBuilder("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 = "\uD83D\uDE0A"; // "😊"という1文字
System.out.println(surrogate); // "😊"
final var sb = new StringBuilder("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
StringBuilder insert (int offset, boolean b)
final var s = "ABCDE";
System.out.println(new StringBuilder(s).insert(0, true)); // "trueABCDE"
System.out.println(new StringBuilder(s).insert(1, true)); // "AtrueBCDE"
System.out.println(new StringBuilder(s).insert(2, true)); // "ABtrueCDE"
System.out.println(new StringBuilder(s).insert(3, true)); // "ABCtrueDE"
System.out.println(new StringBuilder(s).insert(4, true)); // "ABCDtrueE"
System.out.println(new StringBuilder(s).insert(5, true)); // "ABCDEtrue"
//new StringBuilder(s).insert(6, true); // StringIndexOutOfBoundsException
final var sb = new StringBuilder("ABC");
sb.insert(0, false).insert(7, true);
System.out.println(sb); // "falseABtrueC"
StringBuilder insert (int offset, char c)
final var s = "abcde";
System.out.println(new StringBuilder(s).insert(0, 'Z')); // "Zabcde"
System.out.println(new StringBuilder(s).insert(1, 'Z')); // "aZbcde"
System.out.println(new StringBuilder(s).insert(2, 'Z')); // "abZcde"
System.out.println(new StringBuilder(s).insert(3, 'Z')); // "abcZde"
System.out.println(new StringBuilder(s).insert(4, 'Z')); // "abcdZe"
System.out.println(new StringBuilder(s).insert(5, 'Z')); // "abcdeZ"
//new StringBuilder(s).insert(6, 'Z'); // IndexOutOfBoundsException
final var sb = new StringBuilder("abc");
sb.insert(0, 'X').insert(2, 'Y').insert(4, 'Z');
System.out.println(sb); // "XaYbZc"
StringBuilder insert (int offset, char[] str)
final var s = "abcde";
final char[] chars = {'X', 'Y', 'Z'};
System.out.println(new StringBuilder(s).insert(0, chars)); // "XYZabcde"
System.out.println(new StringBuilder(s).insert(1, chars)); // "aXYZbcde"
System.out.println(new StringBuilder(s).insert(2, chars)); // "abXYZcde"
System.out.println(new StringBuilder(s).insert(3, chars)); // "abcXYZde"
System.out.println(new StringBuilder(s).insert(4, chars)); // "abcdXYZe"
System.out.println(new StringBuilder(s).insert(5, chars)); // "abcdeXYZ"
//new StringBuilder(s).insert(6, chars); // StringIndexOutOfBoundsException
StringBuilder insert (int index, char[] str, int offset, int len)
final var s = "abcde";
final char[] chars = {'X', 'Y', 'Z'};
System.out.println(new StringBuilder(s).insert(0, chars, 0, 3)); // "XYZabcde"
System.out.println(new StringBuilder(s).insert(1, chars, 0, 3)); // "aXYZbcde"
System.out.println(new StringBuilder(s).insert(2, chars, 0, 3)); // "abXYZcde"
System.out.println(new StringBuilder(s).insert(3, chars, 0, 3)); // "abcXYZde"
System.out.println(new StringBuilder(s).insert(4, chars, 0, 3)); // "abcdXYZe"
System.out.println(new StringBuilder(s).insert(5, chars, 0, 3)); // "abcdeXYZ"
//new StringBuilder(s).insert(6, chars, 0, 3); // StringIndexOutOfBoundsException
System.out.println(new StringBuilder(s).insert(0, chars, 0, 0)); // "abcde"
System.out.println(new StringBuilder(s).insert(0, chars, 0, 1)); // "Xabcde"
System.out.println(new StringBuilder(s).insert(0, chars, 0, 2)); // "XYabcde"
System.out.println(new StringBuilder(s).insert(0, chars, 0, 3)); // "XYZabcde"
System.out.println(new StringBuilder(s).insert(0, chars, 0, 1)); // "Xabcde"
System.out.println(new StringBuilder(s).insert(0, chars, 1, 1)); // "Yabcde"
System.out.println(new StringBuilder(s).insert(0, chars, 2, 1)); // "Zabcde"
StringBuilder insert (int offset, double d)
final var s = "abcde";
final var d = 1.23;
System.out.println(new StringBuilder(s).insert(0, d)); // "1.23abcde"
System.out.println(new StringBuilder(s).insert(1, d)); // "a1.23bcde"
System.out.println(new StringBuilder(s).insert(2, d)); // "ab1.23cde"
System.out.println(new StringBuilder(s).insert(3, d)); // "abc1.23de"
System.out.println(new StringBuilder(s).insert(4, d)); // "abcd1.23e"
System.out.println(new StringBuilder(s).insert(5, d)); // "abcde1.23"
//new StringBuilder(s).insert(6, d); // StringIndexOutOfBoundsException
final var sb = new StringBuilder("abc");
sb.insert(0, 1.234).insert(6, 5.678e+20);
System.out.println(sb); // "1.234a5.678E20bc"
StringBuilder insert (int offset, float f)
final var s = "abcde";
final var f = 1.23f;
System.out.println(new StringBuilder(s).insert(0, f)); // "1.23abcde"
System.out.println(new StringBuilder(s).insert(1, f)); // "a1.23bcde"
System.out.println(new StringBuilder(s).insert(2, f)); // "ab1.23cde"
System.out.println(new StringBuilder(s).insert(3, f)); // "abc1.23de"
System.out.println(new StringBuilder(s).insert(4, f)); // "abcd1.23e"
System.out.println(new StringBuilder(s).insert(5, f)); // "abcde1.23"
//new StringBuilder(s).insert(6, f); // StringIndexOutOfBoundsException
final var sb = new StringBuilder("abc");
sb.insert(0, 1.234f).insert(6, 5.678e+20f);
System.out.println(sb); // "1.234a5.678E20bc"
StringBuilder insert (int offset, int i)
final var s = "abcde";
final var i = 123;
System.out.println(new StringBuilder(s).insert(0, i)); // "123abcde"
System.out.println(new StringBuilder(s).insert(1, i)); // "a123bcde"
System.out.println(new StringBuilder(s).insert(2, i)); // "ab123cde"
System.out.println(new StringBuilder(s).insert(3, i)); // "abc123de"
System.out.println(new StringBuilder(s).insert(4, i)); // "abcd123e"
System.out.println(new StringBuilder(s).insert(5, i)); // "abcde123"
//new StringBuilder(s).insert(6, i); // StringIndexOutOfBoundsException
final var sb = new StringBuilder("abc");
sb.insert(0, 1234).insert(6, 5678);
System.out.println(sb); // "1234ab5678c"
StringBuilder insert (int offset, long l)
final var s = "abcde";
final var l = 123L;
System.out.println(new StringBuilder(s).insert(0, l)); // "123abcde"
System.out.println(new StringBuilder(s).insert(1, l)); // "a123bcde"
System.out.println(new StringBuilder(s).insert(2, l)); // "ab123cde"
System.out.println(new StringBuilder(s).insert(3, l)); // "abc123de"
System.out.println(new StringBuilder(s).insert(4, l)); // "abcd123e"
System.out.println(new StringBuilder(s).insert(5, l)); // "abcde123"
//new StringBuilder(s).insert(6, l); // StringIndexOutOfBoundsException
final var sb = new StringBuilder("abc");
sb.insert(0, 1234L).insert(6, 1234567890123456789L);
System.out.println(sb); // "1234ab1234567890123456789c"
StringBuilder insert (int dstOffset, CharSequence s)
final var s = "abcde";
final CharSequence seq = new StringBuilder("XYZ");
System.out.println(new StringBuilder(s).insert(0, seq)); // "XYZabcde"
System.out.println(new StringBuilder(s).insert(1, seq)); // "aXYZbcde"
System.out.println(new StringBuilder(s).insert(2, seq)); // "abXYZcde"
System.out.println(new StringBuilder(s).insert(3, seq)); // "abcXYZde"
System.out.println(new StringBuilder(s).insert(4, seq)); // "abcdXYZe"
System.out.println(new StringBuilder(s).insert(5, seq)); // "abcdeXYZ"
//new StringBuilder(s).insert(6, seq); // IndexOutOfBoundsException
StringBuilder insert (int dstOffset, CharSequence s, int start, int end)
final var s = "abcde";
final CharSequence seq = new StringBuilder("XYZ");
System.out.println(new StringBuilder(s).insert(0, seq, 0, 3)); // "XYZabcde"
System.out.println(new StringBuilder(s).insert(1, seq, 0, 3)); // "aXYZbcde"
System.out.println(new StringBuilder(s).insert(2, seq, 0, 3)); // "abXYZcde"
System.out.println(new StringBuilder(s).insert(3, seq, 0, 3)); // "abcXYZde"
System.out.println(new StringBuilder(s).insert(4, seq, 0, 3)); // "abcdXYZe"
System.out.println(new StringBuilder(s).insert(5, seq, 0, 3)); // "abcdeXYZ"
//new StringBuilder(s).insert(6, seq, 0, 3); // IndexOutOfBoundsException
System.out.println(new StringBuilder(s).insert(0, seq, 0, 0)); // "abcde"
System.out.println(new StringBuilder(s).insert(0, seq, 0, 1)); // "Xabcde"
System.out.println(new StringBuilder(s).insert(0, seq, 0, 2)); // "XYabcde"
System.out.println(new StringBuilder(s).insert(0, seq, 0, 3)); // "XYZabcde"
System.out.println(new StringBuilder(s).insert(0, seq, 0, 3)); // "XYZabcde"
System.out.println(new StringBuilder(s).insert(0, seq, 1, 3)); // "YZabcde"
System.out.println(new StringBuilder(s).insert(0, seq, 2, 3)); // "Zabcde"
System.out.println(new StringBuilder(s).insert(0, seq, 3, 3)); // "abcde"
StringBuilder insert (int offset, Object obj)
final var s = "abcde";
final var obj = LocalDate.of(2100, 1, 1);
System.out.println(new StringBuilder(s).insert(0, obj)); // "2100-01-01abcde"
System.out.println(new StringBuilder(s).insert(1, obj)); // "a2100-01-01bcde"
System.out.println(new StringBuilder(s).insert(2, obj)); // "ab2100-01-01cde"
System.out.println(new StringBuilder(s).insert(3, obj)); // "abc2100-01-01de"
System.out.println(new StringBuilder(s).insert(4, obj)); // "abcd2100-01-01e"
System.out.println(new StringBuilder(s).insert(5, obj)); // "abcde2100-01-01"
//new StringBuilder(s).insert(6, obj); // StringIndexOutOfBoundsException
final var sb = new StringBuilder("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"
StringBuilder insert (int offset, String str)
final var s = "abcde";
final var str = "XYZ";
System.out.println(new StringBuilder(s).insert(0, str)); // "XYZabcde"
System.out.println(new StringBuilder(s).insert(1, str)); // "aXYZbcde"
System.out.println(new StringBuilder(s).insert(2, str)); // "abXYZcde"
System.out.println(new StringBuilder(s).insert(3, str)); // "abcXYZde"
System.out.println(new StringBuilder(s).insert(4, str)); // "abcdXYZe"
System.out.println(new StringBuilder(s).insert(5, str)); // "abcdeXYZ"
//new StringBuilder(s).insert(6, str); // StringIndexOutOfBoundsException
int lastIndexOf (String str)
final var sb = new StringBuilder("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 = "\uD83D\uDE0A"; // "😊"という1文字
System.out.println(surrogate); // "😊"
final var sb = new StringBuilder("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 StringBuilder("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 = "\uD83D\uDE0A"; // "😊"という1文字
System.out.println(surrogate); // "😊"
final var sb = new StringBuilder("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 StringBuilder();
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 StringBuilder("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 = "\uD83D\uDE0A"; // "😊"という1文字
System.out.println(surrogate); // "😊"
final var sb = new StringBuilder("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
StringBuilder repeat (int codePoint, int count)
final var codePoint = Character.toCodePoint('\uD83D', '\uDE0A');
System.out.printf("%c%n", codePoint); // 😊
final var sb = new StringBuilder();
System.out.println(sb.repeat(codePoint, 3)); // "😊😊😊"
StringBuilder repeat (CharSequence cs, int count)
final var cs = "Abc";
final var sb = new StringBuilder();
System.out.println(sb.repeat(cs, 3)); // "AbcAbcAbc"
StringBuilder replace (int start, int end, String str)
final var s = "abcd";
System.out.println(new StringBuilder(s).replace(0, 0, "X")); // "Xabcd"
System.out.println(new StringBuilder(s).replace(0, 1, "X")); // "Xbcd"
System.out.println(new StringBuilder(s).replace(0, 2, "X")); // "Xcd"
System.out.println(new StringBuilder(s).replace(0, 3, "X")); // "Xd"
System.out.println(new StringBuilder(s).replace(0, 4, "X")); // "X"
System.out.println(new StringBuilder(s).replace(1, 4, "X")); // "aX"
System.out.println(new StringBuilder(s).replace(2, 4, "X")); // "abX"
System.out.println(new StringBuilder(s).replace(3, 4, "X")); // "abcX"
System.out.println(new StringBuilder(s).replace(4, 4, "X")); // "abcdX"
final var sb = new StringBuilder("abcdef");
sb.replace(0, 2, "AB").replace(3, 6, "DEF");
System.out.println(sb); // "ABcDEF"
StringBuilder reverse ()
final var sb = new StringBuilder("abcdef");
System.out.println(sb.reverse()); // "fedcba"
// サロゲート・コード・ポイントの例です。
final var surrogate = "\uD83D\uDE0A"; // "😊"という1文字
System.out.println(surrogate); // "😊"
final var sb = new StringBuilder("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 StringBuilder("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)
final var sb = new StringBuilder("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 StringBuilder("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 StringBuilder("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 StringBuilder("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)
final var sb = new StringBuilder("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 ()
final var sb = new StringBuilder("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 StringBuilder("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 StringBuilder(" 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で宣言されたメソッド
charAt, chars, codePoints, isEmpty, length, subSequence
「Java API 使用例 : CharSequence」をご参照ください。