Java StringBuffer int length()方法与示例

StringBuffer类insert(int offset,String s) (StringBuffer Class insert(int offset , String s))

  • This method is available in package java.lang.StringBuffer.insert(int offset, String s).

    软件包java.lang.StringBuffer.insert(int offset,String s)中提供了此方法。

  • This method is used to insert the string representation with the given object at the specified position.

    此方法用于在指定位置插入具有给定对象的字符串表示形式。

  • This method is overridable so it is available in different-2 forms like:

    此方法是可重写的,因此有两种不同的形式可用,例如:

    • StringBuffer insert(int offset , boolean b)
    • StringBuffer insert(int offset , char c)
    • StringBuffer insert(int offset , char[] c) etc

Syntax:

句法:

    StringBuffer insert(int offset , String s){
    }

Parameter(s):

参数:

We can pass two or three objects according to our need as a parameter in the method and that object will insert in a sequence at the specified position.

我们可以根据需要传递两个或三个对象作为方法中的参数,并且该对象将按顺序插入到指定位置。

Return value:

返回值:

The return type of this method is StringBuffer that means this method returns a reference to this object.

该方法的返回类型为StringBuffer ,这意味着该方法返回对该对象的引用。

Java程序演示insert()方法的示例 (Java program to demonstrate example of insert() method)

import java.lang.StringBuffer;

public class StringBufferClass {
    public static void main(String[] args) {

        StringBuffer sb = new StringBuffer("Java is a programming language : ");

        // use insert(int offset ,Boolean b) it will insert the Boolean parameter 
        // at index 33 as string to the StringBuffer

        sb.insert(33, true);

        // Display result after inserting

        System.out.println("The result will be after inserting Boolean object to the StringBuffer is :" + sb.toString());

        sb = new StringBuffer("Version of Java is : ");

        // use insert(int offset , int i) it will insert the Integer parameter  
        // at index 21 as string to the StringBuffer

        sb.insert(21, 8);

        // Display result after inserting
        System.out.println("The result will be after inserting Integer object to the StringBuffer is :" + sb.toString());
    }
}

Output

输出量

D:\Programs>javac StringBufferClass.java

D:\Programs>java StringBufferClass
The result will be after inserting Boolean object to the StringBuffer is :Java is a programming language : true
The result will be after inserting Integer object to the StringBuffer is :Version of Java is : 8


翻译自: https://www.includehelp.com/java/stringbuffer-int-length-method-with-example.aspx

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值