📜  Java中的 StringBuffer delete() 方法及示例

📅  最后修改于: 2022-05-13 01:55:11.460000             🧑  作者: Mango

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() 方法:
方案一:

java
// Java program 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);
    }
}


java
// Java program 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);
    }
}


java
// Java program 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);
    }
}


java
// Java program 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);
    }
}


输出:
string buffer = Geeksforgeeks
After deletion string buffer is = Geeksfgeeks

方案二:

Java

// Java program 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

// Java program 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

// Java program 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)