Java StringBuilder delete(int start, int end) Method
delete(int start, int end) method in the StringBuilder class is used to remove a portion of the string, starting from the specified start index to the specified end index. This method is used to modify mutable sequences of characters.
Example 1: The below example demonstrates how to use the delete() method to remove a substring from a given start index to an end index.
// Java Program to Demonstrate the use
// of delete() in StringBuilder
public class Geeks {
public static void main(String[] args)
{
StringBuilder sb = new StringBuilder("Hello, World!");
// Delete substring from index
// 7 to 12 (exclusive)
sb.delete(7, 12);
System.out.println(sb);
}
}
// Java Program to Demonstrate the use
// of delete() in StringBuilder
public class Geeks {
public static void main(String[] args)
{
StringBuilder sb = new StringBuilder("Hello, World!");
// Delete substring from index
// 7 to 12 (exclusive)
sb.delete(7, 12);
System.out.println(sb);
}
}
Output
Hello, !
Syntax of StringBuilder delete() Method
public StringBuilder delete(int start, int end)
Parameters:
- start: It is beginning index from where the deletion will start. The character in the start index is included.
- end: The ending index where the deletion will stop. The character in the end index is excluded.
Return Value: It returns the StringBuilder object after deleting the specified portion of the string.
Example 2: Here, we use the delete() method to delete a substring from a string between a start index and an end index.
// Java program to delete a substring from a string
public class Geeks {
public static void main(String[] args)
{
StringBuilder sb = new StringBuilder("Java Programming");
// Delete substring from
// index 4 to 14 (exclusive)
sb.delete(4, 14);
System.out.println(sb);
}
}
// Java program to delete a substring from a string
public class Geeks {
public static void main(String[] args)
{
StringBuilder sb = new StringBuilder("Java Programming");
// Delete substring from
// index 4 to 14 (exclusive)
sb.delete(4, 14);
System.out.println(sb);
}
}
Output
Javang
Example 3: Here, we use delete() method to delete a substring from a specific index to the end of the string.
// Java program to delete a substring at the end
public class Geeks {
public static void main(String[] args)
{
StringBuilder sb = new StringBuilder("Hello, World!");
// Delete characters from index 7 to the end
sb.delete(7, sb.length());
// Output the modified string
System.out.println(sb);
}
}
// Java program to delete a substring at the end
public class Geeks {
public static void main(String[] args)
{
StringBuilder sb = new StringBuilder("Hello, World!");
// Delete characters from index 7 to the end
sb.delete(7, sb.length());
// Output the modified string
System.out.println(sb);
}
}
Output
Hello,
Example 4: Here, we use delete() to delete the substring from the start of the string to a specific index in the middle.
// Java program to delete a substring
// from the start of the string
public class Geeks {
public static void main(String[] args)
{
StringBuilder sb = new StringBuilder("Delete the first part");
// Delete characters from index 0 to 6 (exclusive)
sb.delete(0, 6);
// Output the modified string
System.out.println(sb);
}
}
// Java program to delete a substring
// from the start of the string
public class Geeks {
public static void main(String[] args)
{
StringBuilder sb = new StringBuilder("Delete the first part");
// Delete characters from index 0 to 6 (exclusive)
sb.delete(0, 6);
// Output the modified string
System.out.println(sb);
}
}
Output
the first part