「Java」setLengthメソッドで文字シーケンスの長さを設定する

2020年11月1日

説明
setLength(int newLength)
文字シーケンスの長さを設定します。
Javaコード

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package com.arkgame.study;
public class StringBuilderLengthDemo {
public static void main(String[] args) {
StringBuilder strA = new StringBuilder("arkgame");
System.out.println("string = " + strA);
//length of StringBuilder
System.out.println("length = " + strA.length());
//set the length of StringBuilder to 5
strA.setLength(5);
//print new StringBuilder value after changing length
System.out.println("After set, string = " + strA);
//length of StringBuilder after changing length
System.out.println("length = " + strA.length());
}
}
package com.arkgame.study; public class StringBuilderLengthDemo { public static void main(String[] args) { StringBuilder strA = new StringBuilder("arkgame"); System.out.println("string = " + strA); //length of StringBuilder System.out.println("length = " + strA.length()); //set the length of StringBuilder to 5 strA.setLength(5); //print new StringBuilder value after changing length System.out.println("After set, string = " + strA); //length of StringBuilder after changing length System.out.println("length = " + strA.length()); } }
package com.arkgame.study;

public class StringBuilderLengthDemo {

      public static void main(String[] args) {
            StringBuilder strA = new StringBuilder("arkgame");
            System.out.println("string = " + strA);

            //length of StringBuilder
            System.out.println("length = " + strA.length());

            //set the length of StringBuilder to 5
            strA.setLength(5);

            //print new StringBuilder value after changing length
            System.out.println("After set, string = " + strA);

            //length of StringBuilder after changing length
            System.out.println("length = " + strA.length());
      }

}

結果
string = arkgame
length = 7
After set, string = arkga
length = 5

Java

Posted by arkgame