StringBuilder delete, reverse

In this chapter you will learn:

  1. How to reverse a StringBuilder
  2. How to delete a sequence of char from a StringBuilder

Reverse a StringBuilder

public class Main {
  public static void main(String[] args) {
    String palindrome = "Dot saw I was Tod";
//java2  s  .  com
    StringBuilder sb = new StringBuilder(palindrome);

    sb.reverse(); // reverse it


The output:

Delete a sequence of char from a StringBuilder

StringBuilder delete(int start, int end) removes the characters in a substring of this sequence.

public class Main {
  public static void main(String[] args) {
    StringBuilder lipsum = new StringBuilder("Lorem ipsum dolor sit amet.");
    System.out.println("lipsum = " + lipsum.toString());
//j a v a 2 s. c om
    lipsum.delete(0, 8);
    System.out.println("lipsum = " + lipsum.toString());

    lipsum.deleteCharAt(lipsum.length() - 1);
    System.out.println("lipsum = " + lipsum.toString());

The output:

Next chapter...

What you will learn in the next chapter:

  1. How to search a string inside a StringBuilder
  2. How to search a StringBuilder from the end
Home » Java Tutorial » String


    Java String type
    Java String Concatenation
    Java String Creation
    Java String Compare
    Java String Search
    Java String and char array
    Java String Conversion
    String trim, length, is empty, and substring
    String replace


    StringBuffer class
    StringBuffer Insert and Append
    StringBuffer length and capacity
    StringBuffer char operation
    StringBuffer Operations
    Search within StringBuffer
    StringBuffer to String


    StringBuilder insert and append
    StringBuilder length and capacity
    StringBuilder get,delete,set char
    StringBuilder delete, reverse
    StringBuilder search with indexOf and lastIndexOf
    StringBuilder to String

String Format

    Formatter class
    Format Specifier
    Format String and characters
    Format integer value
    Format decimal
    Scientific notation format
    Format octal and hexadecimal value
    Format date and time value
    Escape Formatter
    Minimum Field Width
    Specifying Precision
    Format Flags
    Uppercase Option
    Formatter Argument Index
    Align left and right
    Left and right padding a string

String Format Utilities

    Abbreviate string
    Caplitalize a string
    Uncapitalize a string
    Utility class for right padding
    Left padding
    Centers a String
    Transforms words