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

2020年11月1日

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

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