php    php100   android
当前位置:首页 » java.lang包 »

Java.lang.StringBuffer.insert(int offset, char[] str)方法实例

评论  编辑

描述

The java.lang.StringBuffer.insert(int offset, char[] str) method inserts the string representation of the char array argument into this sequence.
The characters of the array argument are inserted into the contents of this sequence at the position indicated by offset. The length of this sequence increases by the length of the argument.

声明

Following is the declaration for java.lang.StringBuffer.insert() method

public StringBuffer insert(int offset, char[] str)

参数

  • offset -- This is the offset.

  • str -- This is the character array.

返回值

This method returns a reference to this object.

异常

  • StringIndexOutOfBoundsException -- if the offset is invalid.

实例一

编辑 +分享实例

以下例子将告诉你如何使用 java.lang.StringBuffer.insert() method.

package gitbook.net;

import java.lang.*;

public class StringBufferDemo {

  public static void main(String[] args) {
  
    StringBuffer buff = new StringBuffer("tutorials website");
    System.out.println("buffer = " + buff);
        
    // character array to be inserted
    char cArr[] = { 'p', 'o', 'i', 'n', 't' };
    // insert character array at offset 9
    buff.insert(9, cArr);
        
    // prints stringbuffer after insertion
    System.out.print("After insertion = ");
    System.out.println(buff.toString());
  }      
}

编译和执行以上程序,将得到以下的结果:

buffer = tutorials website
After insertion = yiibai website

贡献/合作者

正在开放中...
 

评论(条)

  • 还没有评论!