転職を繰り返したサラリーマンの多趣味ブログ

30才未経験でSEに転職した人の多趣味ブログ

【技術書メモ】【パーフェクトJava③】StringBuilderクラスでよく使うメソッド

charAt

// bを出力
String str = "abc";
System.out.println(str.charAt(1));

length

// 3を出力
String str = "abc";
System.out.println(str.length());

subSequence

// cdeを出力
String str = "abcdefg";
System.out.println(str.subSequence(2, 5));

append

// abccdeを出力
StringBuilder sb = new StringBuilder("abc");
System.out.println(sb.append("def"));

delete

// abfghiを出力
StringBuilder sb = new StringBuilder("abcdefghi");
System.out.println(sb.delete(2, 5));

indexOf

// 3を出力
StringBuilder sb = new StringBuilder("abcdefghi");
System.out.println(sb.indexOf("d"));

insert

// abccdefghiを出力
StringBuilder sb = new StringBuilder("abcdefghi");
System.out.println(sb.insert(2, "c"));

replace

// abxxxxfghiを出力
StringBuilder sb = new StringBuilder("abcdefghi");
System.out.println(sb.replace(2, 5, "xxxx"));

setCharAt

// abcxefghiを出力
StringBuilder sb = new StringBuilder("abcdefghi");
sb.setCharAt(3, 'x');
System.out.println(sb);

substring

// cdeを出力
StringBuilder sb = new StringBuilder("abcdefghi");
System.out.println(sb.substring(2, 5));