How do I remove substring from StringBuilder?

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.example.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.toString());

        // 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.toString());

        // 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.toString());

How do I read a text file?

The code snippet below is an example of how to read a text file. This program will read a file called test.txt and shows its content.



public class ReadTextFileExample {
    public static void main(String[] args) {
        File file = new File("/Users/wsaryada/kodejava/test.txt");

        try (BufferedReader reader =
                     new BufferedReader(new FileReader(file))) {
            StringBuilder contents = new StringBuilder();

            String text = null;
            while ((text = reader.readLine()) != null) {

        } catch (IOException e) {

You can also try to use the following example to read a file, How do I read text file content line by line using commons-io?.