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

貢獻/合作者

正在開放中...
 

評論(條)

  • 還冇有評論!