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