Java StringBuilder setLength() 方法



Java StringBuilder setLength() 方法用於設定/更改 StringBuilder 物件的長度。

透過傳遞表示新長度的整數值來呼叫此方法時,當前物件將更改為一個新物件 (StringBuilder),其長度與給定的引數相同。

長度只不過是給定序列中字元的計數。我們在 Java 中有 length() 方法來查詢給定序列的長度。

如果 newLength 引數大於或等於當前長度,則會追加足夠的空字元 ('\u0000'),以便長度變為 newLength 引數。

setLength() 方法接受一個整數引數,該引數儲存 newLength 的值。如果給定的 newLength 值為負數,則會丟擲異常。

語法

以下是 Java StringBuilder setLength() 方法的語法:

public void setLength(int newLength)

引數

  • newLength - 這是新的長度。

返回值

此方法不返回值。

示例:設定 StringBuilder 字串的長度

如果給定的 newLength 引數值為正數,則 setLength() 方法將設定給定序列的新長度。

在下面的程式中,我們使用值為“TutorialsPoint”的值例項化StringBuilder 類。使用setLength() 方法,我們試圖將給定序列的新長度設定為20

public class SetLength {
    public static void main(String[] args) {
        //instantiating the StringBuilder class
        StringBuilder sb = new StringBuilder("TutorialsPoint");
        System.out.println("StringBuilder: " + sb);
        System.out.println("Before setting the new length the original length is: " + sb.length());
        //initialize the newLength argument value
        int newLength = 20;
        System.out.println("The given newLength argument value is: " + newLength);
        //using the setLength() method
        sb.setLength(newLength);
        System.out.println("After setting the new length the string length is: " + sb.length());
    }
}

輸出

執行上述程式後,將產生以下結果:

StringBuilder: TutorialsPoint
Before setting the new length the original length is: 14
The given newLength argument value is: 20
After setting the new length the string length is: 20

示例:在設定 StringBuilder 字串長度時遇到 IndexOutOfBoundsException

如果給定的 newLength 引數值為負數,則 setLength() 方法將丟擲IndexOutOfBoundsException

在下面的示例中,我們使用值“HelloWorld”建立一個StringBuilder 類的物件。使用 setLength() 方法,我們試圖將序列長度設定為 -5

public class SetLength {
    public static void main(String[] args) {
        try {
            //create an object of the StringBuilder class
            StringBuilder sb = new StringBuilder("HelloWorld");
            System.out.println("StringBuilder: " + sb);
            System.out.println("Before setting the original length is: " + sb.length());
            //initialize the newLength argument value
            int newLength = -5;
            System.out.println("The given newLength argument value is: " + newLength);
            //using the setLength() method
            sb.setLength(newLength);
            System.out.println("After setting is the new length of string is: " + sb.length());
        } catch(IndexOutOfBoundsException e) {
            e.printStackTrace();
            System.out.println("Exception: " + e);
        }
    }
}

輸出

以下是上述程式的輸出:

StringBuilder: HelloWorld
Before setting the original length is: 10
The given newLength argument value is: -5
java.lang.StringIndexOutOfBoundsException: String index out of range: -5
   at java.base/java.lang.AbstractStringBuilder.setLength(AbstractStringBuilder.java:312)
   at java.base/java.lang.StringBuilder.setLength(StringBuilder.java:234)
   at SetLength.main(SetLength.java:14)
Exception: java.lang.StringIndexOutOfBoundsException: String index out of range: -5

示例:設定 StringBuilder 字串的長度

如果給定字串長度為,此方法將設定給定序列的新長度。

在下面的程式中,我們使用空值例項化StringBuilder 類。然後,使用setLength() 方法,我們試圖將給定空序列的新長度設定為5

public class SetLength {
    public static void main(String[] args) {
        //instantiating the StringBuilder class
        StringBuilder sb = new StringBuilder();
        System.out.println("StringBuilder: " + sb);
        System.out.println("Before setting the new length the original length is: " + sb.length());
        //initialize the newLength argument value
        int newLength = 5;
        System.out.println("The given newLength argument value is: " + newLength);
        //using the setLength() method
        sb.setLength(newLength);
        System.out.println("After setting the new length the string length is: " + sb.length());
    }
}

輸出

上述程式產生以下結果:

StringBuilder:
Before setting the new length the original length is: 0
The given newLength argument value is: 5
After setting the new length the string length is: 5
java_lang_stringbuilder.htm
廣告