Java StringBuffer void setLength(int newlen)方法与示例

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值