当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java StringBuffer delete()用法及代码示例


java.lang.StringBuffer.delete()是Java中的内置方法,用于删除或删除此序列的子字符串中的字符。子字符串从指定的索引start_point开始,并扩展到在索引end_point处的字符。

用法:

public StringBuffer delete(int start_point, int end_point)

参数:该方法接受两个整数类型的参数:
start_point–这是指起始索引,并包含在计数中。
end_point–这指的是结束索引,不包括在计数中。


返回值:在删除参数中提到的范围所形成的子字符串后,该方法将返回字符串。

异常:如果start_point为负,大于length(),或者大于end_point,则发生StringIndexOutOfBoundsException。

例子:

Input: String = "Apple"
            start_point = 2 
            end_point = 4

Output: Ape

Input: String = "GeeksforGeeks"
       start_point = 2 
       end_point = 7

Output: GerGeeks

以下程序说明了java.lang.StringBuffer.delete()方法:
示例1:

// Java praogram to illustrate the 
// java.lang.StringBuffer.delete() 
import java.lang.*; 
  
public class geeks { 
  
    public static void main(String[] args) 
    { 
  
        StringBuffer sbf = new StringBuffer("Geeksforgeeks"); 
        System.out.println("string buffer = " + sbf); 
  
        // Deleting characters from index 2 to 7 
        sbf.delete(6, 8); 
        System.out.println("After deletion string buffer is = " + sbf); 
    } 
}
输出:
string buffer = Geeksforgeeks
After deletion string buffer is = Geeksfgeeks

示例2:

// Java praogram to illustrate the 
// java.lang.StringBuffer.delete() 
import java.lang.*; 
  
public class geeks { 
  
    public static void main(String[] args) 
    { 
  
        StringBuffer sbf = new StringBuffer("Welcome to Geeksforgeeks"); 
        System.out.println("string buffer = " + sbf); 
  
        // deleting characters from index 5 to index 9 
        sbf.delete(5, 9); 
        System.out.println("After deletion string buffer is = " + sbf); 
    } 
}
输出:
string buffer = Welcome to Geeksforgeeks
After deletion string buffer is = Welcoo Geeksforgeeks

示例3:此处索引为负。

// Java praogram to illustrate the 
// java.lang.StringBuffer.delete() 
import java.lang.*; 
  
public class geeks { 
  
    public static void main(String[] args) 
    { 
  
        StringBuffer sbf = new StringBuffer("Welcome to Geeksforgeeks"); 
        System.out.println("string buffer = " + sbf); 
  
        sbf.delete(-5, 9); 
        System.out.println("After deletion string buffer is = " + sbf); 
    } 
}
输出:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: 
                                         String index out of range: -5
    at java.lang.AbstractStringBuilder.delete(AbstractStringBuilder.java:756)
    at java.lang.StringBuffer.delete(StringBuffer.java:430)
    at geeks.main(geeks.java:13)

示例4:此处没有索引。

// Java praogram to illustrate the 
// java.lang.StringBuffer.delete() 
import java.lang.*; 
  
public class geeks { 
  
    public static void main(String[] args) 
    { 
  
        StringBuffer sbf = new StringBuffer("Welcome to Geeksforgeeks"); 
        System.out.println("string buffer = " + sbf); 
  
        sbf.delete(99, 109); 
        System.out.println("After deletion string buffer is = " + sbf); 
    } 
}
输出:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException
    at java.lang.AbstractStringBuilder.delete(AbstractStringBuilder.java:760)
    at java.lang.StringBuffer.delete(StringBuffer.java:430)
    at geeks.main(geeks.java:13)


相关用法


注:本文由纯净天空筛选整理自ankita_chowrasia大神的英文原创作品 StringBuffer delete() Method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。