Java StringBuffer.setLength()方法

Java StringBuffer.setLength()方法

Java StringBuffer.setLength()方法用法实例教程,
width="728" height="90" frameborder="0" marginwidth="0" marginheight="0" vspace="0" hspace="0" allowtransparency="true" scrolling="no" allowfullscreen="true" id="aswift_0" name="aswift_0" style="box-sizing: border-box; font-size: 16px; left: 0px; position: absolute; top: 0px;">
id="cproIframe_u1064372_1" width="728" height="90" src="http://pos.baidu.com/acom?adn=3&at=38&aurl=&cad=1&ccd=24&cec=UTF-8&cfv=16&ch=0&col=zh-CN&conOP=0&cpa=1&dai=1&dis=0&ltr=https%3A%2F%2Fwww.baidu.com%2Flink%3Furl%3DzBZhLEQSVlBCyecgk8rzh57Aqoq0r4PBhOcxxyM9K6eiiUxqN_-9jwReMeaPRlwNaXIJqfASdFPOeADi5IgdaGkHAaRxbHiTo21y4IIjEvS%26wd%3D%26eqid%3Db50fd25800061fb60000000555ecd9c6&ltu=http%3A%2F%2Fwww.yiibai.com%2Fjavalang%2Fstringbuffer_setlength.html&lunum=6&n=90029059_cpr&pcs=1093x533&pis=10000x10000&ps=304x303&psr=1366x768&pss=1093x1462&qn=7ed92043135cbc4b&rad=&rsi0=728&rsi1=90&rsi5=4&rss0=%23FFFFFF&rss1=%23FFFFFF&rss2=%230000FF&rss3=%23444444&rss4=%23008000&rss5=&rss6=%23e10900&rss7=&scale=&skin=&td_id=1064372&tn=text_default_728_90&tpr=1441585346923&ts=1&version=2.0&xuanting=0&dtm=BAIDU_DUP2_SETJSONADSLOT&dc=2&di=u1064372&ti=Java%20StringBuffer.setLength()%E6%96%B9%E6%B3%95&tt=1441585346898.26.138.141" align="center,center" marginwidth="0" marginheight="0" scrolling="no" frameborder="0" allowtransparency="true" style="box-sizing: border-box; font-size: 16px;">

描述

java.lang.StringBuffer.setLength() 方法设置的字符序列的长度。该序列被改变到一个新的字符序列的参数所指定的长度。
If the 如果newLength参数,是大于或等于当前长度,足够的空字符('\ u0000')被追加使长度变newLength参数

声明

以下是java.lang.StringBuffer.setLength()方法的声明

public void setLength(int newLength)

参数

  • newLength -- 这是新的长度.

返回值

此方法不返回任何值

异常

  • IndexOutOfBoundsException -- 如果newLength参数是负.

实例

下面的例子说明了如何使用java.lang.StringBuffer.setLength()方法

package com.yiibai;

import java.lang.*;

public class StringBufferDemo {
 
  public static void main(String[] args) {
  
    StringBuffer buff = new StringBuffer("tutorials");
    System.out.println("buffer1 = " + buff);  
    // length of stringbuffer
    System.out.println("length = " + buff.length());
        
    // set the length of stringbuffer to 5
    buff.setLength(5);
        
    // print new stringbuffer value after changing length
    System.out.println("buffer2 = " + buff);
    // length of stringbuffer after changing length
    System.out.println("length = " + buff.length());
  }
}

让我们来编译和运行上面的程序,这将产生以下结果:

buffer1 = tutorials
length = 9
buffer2 = tutor
length = 5
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值