java.lang.StringBuffer.deleteCharAt()方法實例
java.lang.StringBuffer.deleteCharAt() 方法移除char在此序列中的指定位置。這個序列是由一個字符縮短。
聲明
以下是java.lang.StringBuffer.deleteCharAt()方法的聲明
public StringBuffer deleteCharAt(int index)
參數
-
index -- 這是為移除char的索引。
返回值
This method returns this object.
異常
-
StringIndexOutOfBoundsException -- 如果索引是負值或大於或等於length()。
例子
下麵的例子顯示java.lang.StringBuffer.deleteCharAt()方法的使用。
package com.yiibai; import java.lang.*; public class StringBufferDemo { public static void main(String[] args) { StringBuffer buff = new StringBuffer("Java lang package"); System.out.println("buffer = " + buff); // deleting character from index 4 to index 9 buff.delete(4, 9); System.out.println("After deletion = " + buff); buff = new StringBuffer("amit"); System.out.println("buffer = " + buff); // deleting character at index 2 buff.deleteCharAt(2); System.out.println("After deletion = " + buff); } }
讓我們來編譯和運行上麵的程序,這將產生以下結果:
buffer = Java lang package After deletion = Java package buffer = amit After deletion = amt