StringBuilder delete, reverse
In this chapter you will learn:
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
System.out.println(sb);
}
}
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:
Home » Java Tutorial » String