在Java中更改StringBuffer对象的长度
setLength(intnewLength)方法用于更改StringBuffer对象的长度。它设置字符序列的长度。字符序列更新为新的字符序列,其长度由方法中作为参数传递的值确定。newLength应该大于或等于零。
声明java.lang.StringBuffer(intnewLength)方法如下-
public void setLength(int newLength)
让我们看一个示例程序,该示例说明setLength(intnewLength)方法的用法
示例
public class Example {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer("Hello World");
System.out.println(sb);
System.out.println("Original length : "+sb.length());
System.out.println("Original capacity : "+sb.capacity());
sb.setLength(5); // changing the length of the StringBuffer object
System.out.println();
System.out.println(sb);
System.out.println("New length : " +sb.length());
System.out.println("New capacity : " +sb.capacity());
}
}输出结果
Hello World Original length : 11 Original capacity : 27 Hello New length : 5 New capacity : 27
热门推荐
10 儿子立冬祝福语简短独特
11 对当兵的祝福语简短
12 侄儿高考试祝福语简短
13 伴郎红包祝福语朋友简短
14 媳妇生日简短祝福语朋友
15 公司年会祝福语简短最好
16 元旦感恩祝福语简短大全
17 红包祝福语简短10字
18 周六早晨祝福语简短