StringBuilder类的delete(int start,int end)方法从StringBuilder包含的String中删除从索引start到index end-1的字符。此方法使用两个索引作为参数,第一个开始代表第一个字符的索引,结束索引代表要从StringBuilder包含的String中删除的子字符串的最后一个字符之后的索引,并将剩余的String作为StringBuilder对象返回。
用法:
public StringBuilder delete(int start, int end)
参数:此方法接受两个参数:
- start:子字符串的第一个字符的索引。
- end:子字符串最后一个字符之后的索引。
返回值:删除子字符串后,此方法返回此StringBuilder对象。
异常:如果start小于零,或者start大于String的长度,或者start大于end,则此方法引发StringIndexOutOfBoundsException。
下面的程序演示StringBuilder类的delete()方法:
示例1:
// Java program to demonstrate
// the delete() 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 substring from index 2 to 8
StringBuilder afterRemoval = str.delete(2, 8);
// print string after removal of substring
System.out.println("After removal String = "
+ afterRemoval.toString());
}
}
输出:
Before removal String = WelcomeGeeks After removal String = Weeeks
示例2:
// Java program to demonstrate
// the delete() 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 substring from index 8 to 8
StringBuilder afterRemoval = str.delete(8, 8);
// start equal to end so no change in string
// print string after removal of substring
System.out.println("After removal of SubString"
+ " start=8 to end=8"
+ " String becomes => "
+ afterRemoval.toString());
// remove the substring from index 1 to 8
afterRemoval = str.delete(1, 8);
// print string after removal of substring
System.out.println("After removal of SubString"
+ " start=1 to end=8"
+ " String becomes => "
+ afterRemoval.toString());
}
}
输出:
Before removal String = GeeksforGeeks After removal of SubString start=8 to end=8 String becomes => GeeksforGeeks After removal of SubString start=1 to end=8 String becomes => GGeeks
示例3:演示IndexOutOfBoundException
// Java program to demonstrate
// exception thrown by the delete() Method.
class GFG {
public static void main(String[] args)
{
// create a StringBuilder object
// with a String pass as parameter
StringBuilder
str
= new StringBuilder("GeeksforGeeks");
try {
// make start greater than end
StringBuilder afterRemoval = str.delete(7, 4);
}
catch (Exception e) {
System.out.println("Exception: " + e);
}
}
}
输出:
Exception: java.lang.StringIndexOutOfBoundsException
参考:
https://docs.oracle.com/javase/10/docs/api/java/lang/StringBuilder.html#delete(int, int)
相关用法
- Java StringBuilder reverse()用法及代码示例
- Java StringBuilder setCharAt()用法及代码示例
- Java StringBuilder replace()用法及代码示例
- Java StringBuilder codePointCount()用法及代码示例
- Java StringBuilder charAt()用法及代码示例
- Java StringBuilder codePointAt()用法及代码示例
- Java StringBuilder subSequence()用法及代码示例
- Java StringBuilder setLength()用法及代码示例
- Java StringBuilder ensureCapacity()用法及代码示例
- Java StringBuilder length()用法及代码示例
- Java StringBuilder capacity()用法及代码示例
- Java StringBuilder getChars()用法及代码示例
- Java StringBuilder codePointBefore()用法及代码示例
- Java StringBuilder deleteCharAt()用法及代码示例
注:本文由纯净天空筛选整理自AmanSingh2210大神的英文原创作品 StringBuilder delete() in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。