StringBuffer类void setLength(int newlen) (StringBuffer Class void setLength(int newlen))
This method is available in package java.lang.StringBuffer.setLength(int newlen).
软件包java.lang.StringBuffer.setLength(int newlen)中提供了此方法。
This method is used to sets the length of the new StringBuffer or in other words we can resize the StringBuffer if we want.
此方法用于设置新StringBuffer的长度,换句话说,如果需要,我们可以调整StringBuffer的大小。
This method will raise an exception if the new buffer length is greater than or equal to the length of the current StringBuffer object.
如果新的缓冲区长度大于或等于当前StringBuffer对象的长度,则此方法将引发异常。
Syntax:
句法:
void setLength(int newlen){
}
Parameter(s):
参数:
We pass only one object in the method of the StringBuffer i.e. newlength(newlen).
我们仅在StringBuffer方法中传递一个对象,即newlength(newlen)。
Return value:
返回值:
The return type of this method is void that means this method returns nothing.
此方法的返回类型为void ,表示此方法不返回任何内容。
Java程序演示setLength()方法的示例 (Java program to demonstrate example of setLength() method)
import java.lang.StringBuffer;
public class StringBufferClass {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer(" java.lang is a package ");
// Display Current StringBuffer object sb
System.out.println("The current StringBuffer is : " + sb);
// Display Current length of the StringBuffer object sb
System.out.println("The current length of StringBuffer object is : " + sb.length());
// Implement setLength(int newlen) update the new length of the StringBuffer
sb.setLength(9);
System.out.println("The updated length of the StringBuffer object is : " + sb.length());
// Display Updated StringBuffer object sb
System.out.println("The updated StringBuffer object is :" + sb);
}
}
Output
输出量
D:\Programs>javac StringBufferClass.java
D:\Programs>java StringBufferClass
The current StringBuffer is : java.lang is a package
The current length of StringBuffer object is : 23
The updated length of the StringBuffer object is : 9
The updated StringBuffer object is :java.lang
翻译自: https://www.includehelp.com/java/stringbuffer-void-setlength-int-newlen-method-with-example.aspx