StringBuilder类的deleteCharAt(int index)方法从StringBuilder包含的String中删除给定索引处的字符。此方法将index作为参数,该参数表示我们要删除的char的索引,并将剩余的String作为StringBuilder对象返回。应用此方法后,此StringBuilder缩短一个字符。
用法:
public StringBuilder deleteCharAt(int index)
参数:此方法接受一个参数索引,该参数代表要删除的字符的索引。
返回值:删除字符后,此方法返回此StringBuilder对象。
异常:如果索引小于零或索引大于String的长度,则此方法将引发StringIndexOutOfBoundsException。
下面的程序演示StringBuilder类的deleteCharAt()方法:
示例1:
// Java program to demonstrate
// the deleteCharAt() Method.
class GFG {
public static void main(String[] args)
{
// create a StringBuilder object
// with a String pass as parameter
StringBuilder
str
= new StringBuilder("WelcomeGeeks");
// print string
System.out.println("Before removal String = "
+ str.toString());
// remove the Character from index 8
StringBuilder afterRemoval = str.deleteCharAt(8);
// print string after removal of Character
System.out.println("After removal of character"
+ " at index 8 = "
+ afterRemoval.toString());
}
}
输出:
Before removal String = WelcomeGeeks After removal of character at index 8 = WelcomeGeks
示例2:
// Java program to demonstrate
// the deleteCharAt() Method.
class GFG {
public static void main(String[] args)
{
// create a StringBuilder object
// with a String pass as parameter
StringBuilder
str
= new StringBuilder("GeeksforGeeks");
// print string
System.out.println("Before removal String = "
+ str.toString());
// remove the Character from index 3
str = str.deleteCharAt(3);
// print string after removal of Character
System.out.println("After removal of Character"
+ " from index=3"
+ " String becomes => "
+ str.toString());
// remove the substring from index 5
str = str.deleteCharAt(5);
// print string after removal of Character
System.out.println("After removal of Character"
+ " from index=5"
+ " String becomes => "
+ str.toString());
}
}
输出:
Before removal String = GeeksforGeeks After removal of Character from index=3 String becomes => GeesforGeeks After removal of Character from index=5 String becomes => GeesfrGeeks
示例3:删除IndexOutOfBoundException
// Java program to demonstrate
// exception thrown by the deleteCharAt() Method.
class GFG {
public static void main(String[] args)
{
// create a StringBuilder object
// with a String pass as parameter
StringBuilder
str
= new StringBuilder("evil dead_01");
try {
// make index > length of String
StringBuilder
afterRemoval
= str.deleteCharAt(14);
}
catch (Exception e) {
System.out.println("Exception: " + e);
}
}
}
输出:
Exception: java.lang.StringIndexOutOfBoundsException: String index out of range: 14
参考:
https://docs.oracle.com/javase/10/docs/api/java/lang/StringBuilder.html#deleteCharAt(int)
相关用法
- Java StringBuffer deleteCharAt()用法及代码示例
- Java StringBuilder delete()用法及代码示例
- Java StringBuilder getChars()用法及代码示例
- Java StringBuilder length()用法及代码示例
- Java StringBuilder ensureCapacity()用法及代码示例
- Java StringBuilder setCharAt()用法及代码示例
- Java StringBuilder Class用法及代码示例
- Java StringBuilder reverse()用法及代码示例
- Java StringBuilder setLength()用法及代码示例
- Java StringBuilder subSequence()用法及代码示例
- Java StringBuilder capacity()用法及代码示例
- Java StringBuilder codePointBefore()用法及代码示例
- Java StringBuilder codePointCount()用法及代码示例
- Java StringBuilder charAt()用法及代码示例
- Java StringBuilder codePointAt()用法及代码示例
注:本文由纯净天空筛选整理自AmanSingh2210大神的英文原创作品 StringBuilder deleteCharAt() in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。