Java StringBuffer 類的deleteCharAt(int index) 方法用於刪除該序列指定位置的字符。刪除一個字符後,當前序列縮短一個字符。
用法:
public StringBuffer deleteCharAt(int index)
參數:
數據類型 | 參數 | 描述 |
---|---|---|
int | index | 索引是要刪除的字符的位置。 |
返回值:
此 deleteCharAt(int index) 方法返回此對象。
異常:
StringIndexOutOfBoundsException - 如果索引為負或大於或等於字符串緩衝區的 length()。
兼容版本:
Java 1.2 及以上
例子1
public class StringBufferDeleteCharAtExample1 {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer("javatpoint");
System.out.println("string1:" + sb);
// deleting the character at index 4
sb = sb.deleteCharAt(4);
System.out.println("After deleting:" + sb);
sb = new StringBuffer("hello java");
System.out.println("string2:" + sb);
// deleting the character at index 5
sb = sb.deleteCharAt(5);
System.out.println("After deleting:" + sb);
}
}
輸出:
string1:javatpoint After deleting:javapoint string2:hello java After deleting:hellojava
例子2
此 deleteCharAt(int) 方法在提供負索引值時拋出異常。
public class StringBufferDeleteCharAtExample2 {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer("javatpoint");
System.out.println("string1:" + sb);
// try to delete the character at index value negative
sb = sb.deleteCharAt(-1);
System.out.println("After deleting:" + sb);
}
}
輸出:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException:String index out of range:-1 at java.lang.AbstractStringBuilder.deleteCharAt(Unknown Source) at java.lang.StringBuffer.deleteCharAt(Unknown Source) at StringBufferDeleteCharAtExample2.main(StringBufferDeleteCharAtExample2.java:6) string1 = javatpoint
例子3
刪除 Character.charCount(thisSequence.codePointAt(index)) 方法的索引值處的字符。
public class StringBufferDeleteCharAtExample3 {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer("javatpoint");
System.out.println("string:" + sb);
// deleting string buffer using character count at code point
int cp = sb.codePointAt(0);
System.out.println("codepoint at 0:"+cp);
int totalCharacter =Character.charCount(cp);
System.out.println("total character at codepoint "+cp+":" + totalCharacter);
sb = sb.deleteCharAt(totalCharacter);
System.out.println("after deleting at index "+totalCharacter +":"+ sb);
}
}
輸出:
string:javatpoint codepoint at 0:106 total character at codepoint 106:1 after deleting at index 1:jvatpoint
示例 4
在這個例子中,我們從用戶那裏獲取輸入字符串和字符索引。
import java.util.Scanner;
public class StringBufferDeleteCharAtExample4 {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer("");
System.out.print("enter your string value:");
Scanner sc = new Scanner(System.in);
sb.append(sc.nextLine());
System.out.print("enter index:");
int index = sc.nextInt();
// deleting the character at input index
System.out.println("after deleting resultant string:"+sb.deleteCharAt(index));
sc.close();
}
}
輸出:
enter your string value:hello string enter index:3 after deleting resultant string:helo string
相關用法
- Java StringBuffer deleteCharAt()用法及代碼示例
- Java StringBuffer delete()用法及代碼示例
- Java StringBuffer toString()用法及代碼示例
- Java StringBuffer offsetByCodePoints()用法及代碼示例
- Java StringBuffer ensureCapacity()用法及代碼示例
- Java StringBuffer subSequence()用法及代碼示例
- Java StringBuffer codePointAt()用法及代碼示例
- Java StringBuffer setLength()用法及代碼示例
- Java StringBuffer replace()用法及代碼示例
- Java StringBuffer trimToSize()用法及代碼示例
- Java StringBuffer appendCodePoint()用法及代碼示例
- Java StringBuffer getChars()用法及代碼示例
- Java StringBuffer indexOf()用法及代碼示例
- Java StringBuffer reverse()用法及代碼示例
- Java StringBuffer codePointCount()用法及代碼示例
- Java StringBuffer insert()用法及代碼示例
- Java StringBuffer lastIndexOf()用法及代碼示例
- Java StringBuffer substring()用法及代碼示例
- Java StringBuffer append()用法及代碼示例
- Java StringBuffer codePointBefore()用法及代碼示例
注:本文由純淨天空篩選整理自 Java StringBuffer deleteCharAt(int index) method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。