「Java」StringBuilder.append()で文字列を追加する

2021年11月3日

書式
StringBuilder 変数名 = new StringBuilder(文字列);
変数名.append(追加文字列");

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package com.arkgame.study;
public class AppendDemo {
public static void main(String[] args) {
StringBuilder cft = new StringBuilder("tokyo");
cft.append("2020");
System.out.println(cft.toString());
StringBuffer sr = new StringBuffer(" hello world ");
cft.append(sr);
System.out.println(cft.toString());
boolean bn = false;
cft.append(bn);
System.out.println(cft.toString());
double dd = 23456.01;
cft.append(dd);
System.out.println(cft.toString());
char ch = '2';
cft.append(ch);
System.out.println(cft.toString());
char[] cA = { 'b', 'c' };
cft.append(cA);
System.out.println(cft.toString());
}
}
package com.arkgame.study; public class AppendDemo { public static void main(String[] args) { StringBuilder cft = new StringBuilder("tokyo"); cft.append("2020"); System.out.println(cft.toString()); StringBuffer sr = new StringBuffer(" hello world "); cft.append(sr); System.out.println(cft.toString()); boolean bn = false; cft.append(bn); System.out.println(cft.toString()); double dd = 23456.01; cft.append(dd); System.out.println(cft.toString()); char ch = '2'; cft.append(ch); System.out.println(cft.toString()); char[] cA = { 'b', 'c' }; cft.append(cA); System.out.println(cft.toString()); } }
package com.arkgame.study;

public class AppendDemo {

      public static void main(String[] args) {
            StringBuilder cft = new StringBuilder("tokyo");
            cft.append("2020");
            System.out.println(cft.toString());

            StringBuffer sr = new StringBuffer(" hello world ");
            cft.append(sr);
            System.out.println(cft.toString());

            boolean bn = false;
            cft.append(bn);
            System.out.println(cft.toString());

            double dd = 23456.01;
            cft.append(dd);
            System.out.println(cft.toString());

            char ch = '2';
            cft.append(ch);
            System.out.println(cft.toString());

            char[] cA = { 'b', 'c' };
            cft.append(cA);
            System.out.println(cft.toString());

      }

}

 

結果

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
tokyo2020
tokyo2020 hello world
tokyo2020 hello world false
tokyo2020 hello world false23456.01
tokyo2020 hello world false23456.012
tokyo2020 hello world false23456.012bc
tokyo2020 tokyo2020 hello world tokyo2020 hello world false tokyo2020 hello world false23456.01 tokyo2020 hello world false23456.012 tokyo2020 hello world false23456.012bc
tokyo2020
tokyo2020 hello world
tokyo2020 hello world false
tokyo2020 hello world false23456.01
tokyo2020 hello world false23456.012
tokyo2020 hello world false23456.012bc

 

Java

Posted by arkgame