- Java.lang 包類
- Java.lang - 首頁
- Java.lang - Boolean
- Java.lang - Byte
- Java.lang - Character
- Java.lang - Character.Subset
- Java.lang - Character.UnicodeBlock
- Java.lang - Class
- Java.lang - ClassLoader
- Java.lang - Compiler
- Java.lang - Double
- Java.lang - Enum
- Java.lang - Float
- Java.lang - InheritableThreadLocal
- Java.lang - Integer
- Java.lang - Long
- Java.lang - Math
- Java.lang - Number
- Java.lang - Object
- Java.lang - Package
- Java.lang - Process
- Java.lang - ProcessBuilder
- Java.lang - Runtime
- Java.lang - RuntimePermission
- Java.lang - SecurityManager
- Java.lang - Short
- Java.lang - StackTraceElement
- Java.lang - StrictMath
- Java.lang - String
- Java.lang - StringBuffer
- Java.lang - StringBuilder
- Java.lang - System
- Java.lang - Thread
- Java.lang - ThreadGroup
- Java.lang - ThreadLocal
- Java.lang - Throwable
- Java.lang - Void
- Java.lang 包補充
- Java.lang - 介面
- Java.lang - 錯誤
- Java.lang - 異常
- Java.lang 包有用資源
- Java.lang - 有用資源
- Java.lang - 討論
Java StringBuffer setLength() 方法
Java StringBuffer setLength() 方法用於設定/更改 StringBuffer 物件的長度。
透過傳遞表示新長度的整數值來呼叫此方法時,當前物件將更改為一個新的物件(StringBuffer),其長度與給定的引數相同。
長度只不過是給定序列中字元的計數。我們在 Java 中有 length() 方法來查詢給定序列的長度。
如果 newLength 引數大於或等於當前長度,則會附加足夠的空字元 ('\u0000'),以使長度變為 newLength 引數。
setLength() 方法接受一個整型引數,該引數儲存 newLength 的值。如果給定的 newLength 值為負數,則會丟擲異常。
語法
以下是 Java StringBuffer setLength() 方法的語法:
public void setLength(int newLength)
引數
- newLength − 這是新的長度。
返回值
此方法不返回值。
示例:設定 StringBuffer 字串的長度
如果給定的 newLength 引數值為正數,則 setLength() 方法將設定給定序列的新長度。
在下面的程式中,我們使用值“TutorialsPoint”例項化StringBuffer 類。使用setLength() 方法,我們嘗試將給定序列的新長度設定為20。
public class SetLength {
public static void main(String[] args) {
//instantiating the StringBuffer class
StringBuffer sb = new StringBuffer("TutorialsPoint");
System.out.println("StringBuffer: " + 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());
}
}
輸出
執行上述程式後,將產生以下結果:
StringBuffer: 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
示例:在設定 StringBuffer 字串長度時遇到 IndexOutOfBoundsException
如果給定的 newLength 引數值為負數,則 setLength() 方法將丟擲IndexOutOfBoundsException。
在下面的示例中,我們使用值“HelloWorld”建立一個StringBuffer 類的物件。使用 setLength() 方法,我們嘗試將序列長度設定為 -5。
public class SetLength {
public static void main(String[] args) {
try {
//create an object of the StringBuffer class
StringBuffer sb = new StringBuffer("HelloWorld");
System.out.println("StringBuffer: " + 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);
}
}
}
輸出
以下是上述程式的輸出:
StringBuffer: 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.StringBuffer.setLength(StringBuffer.java:234) at SetLength.main(SetLength.java:14) Exception: java.lang.StringIndexOutOfBoundsException: String index out of range: -5
示例:設定 StringBuffer 字串的長度
如果給定字串長度為零,則此方法將設定給定序列的新長度。
在下面的程式中,我們使用空值例項化StringBuffer 類。然後,使用setLength() 方法,我們嘗試將給定空序列的新長度設定為5。
public class SetLength {
public static void main(String[] args) {
//instantiating the StringBuffer class
StringBuffer sb = new StringBuffer();
System.out.println("StringBuffer: " + 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());
}
}
輸出
上述程式產生以下結果:
StringBuffer: 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