This example demonstrate you how to use the StringBuilder
delete(int start, int end)
and deleteCharAt(int index)
to remove a substring or a single character from a StringBuilder
.
package org.kodejava.lang;
public class StringBuilderDelete {
public static void main(String[] args) {
StringBuilder lipsum = new StringBuilder("Lorem ipsum dolor sit " +
"amet, consectetur adipisicing elit.");
System.out.println("lipsum = " + lipsum);
// We'll remove a substring from this StringBuilder starting from
// the first character to the 28th character.
lipsum.delete(0, 28);
System.out.println("lipsum = " + lipsum);
// Removes a char from the StringBuilder. In the example below we
// remove the last character.
lipsum.deleteCharAt(lipsum.length() - 1);
System.out.println("lipsum = " + lipsum);
}
}
The result of the code snippet above:
lipsum = Lorem ipsum dolor sit amet, consectetur adipisicing elit.
lipsum = consectetur adipisicing elit.
lipsum = consectetur adipisicing elit
Latest posts by Wayan (see all)
- How do I get number of each day for a certain month in Java? - September 8, 2024
- How do I get operating system process information using ProcessHandle? - July 22, 2024
- How do I sum a BigDecimal property of a list of objects using Java Stream API? - July 22, 2024