Top community members
All Wiki Articles Create Wiki Article

In the past we had a dream,
to have place where we could share IT knowledge,
to ask questions without fear that someone will judge us.

Now we are a group of people who make this dream come true. ❀ đŸ’»

If you think sharing knowledge and helping other is valuable.

join our community - Click here

Java - Remove last character from string builder in java

0 contributions
9 points

Remove last character from StringBuilder in Java is simple. StringBuilder has build in method deleteCharAt(). As a parameter we pass current length of our StringBuilder.

Syntax:

StringBuilder builder = new StringBuilder();

builder.deleteCharAt( builder.length() - 1 );

1. Remove last char

public class Example {

    public static void main(String[] args) {

        StringBuilder builder = new StringBuilder();

        builder.append("1234");
        System.out.println( builder.toString() ); // 1234

        builder.deleteCharAt( builder.length() - 1 );
        System.out.println( builder.toString() ); // 123
    }
}

2. Safe way to remove last char

In order to saftly remove last character from StringBuilder in java, we need to check the length of StringBuilder. In case we won't do it and the StringBuilder will be longer we get:

java.lang.StringIndexOutOfBoundsException

Example with safe way of how to remove last char:

public class Example {

    public static void main(String[] args) {

        StringBuilder builder = new StringBuilder();

        builder.append("1234");
        System.out.println( builder.toString() ); // 1234

        removeLastChar(builder);
        System.out.println( builder.toString() ); // 123
    }

    public static void removeLastChar(StringBuilder builder) {
        if (builder.length() > 0) {
            builder.deleteCharAt( builder.length() - 1 );
        }
    }
}

This method allow us to avoid the below exception:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: 
String index out of range: -1
	at java.lang.AbstractStringBuilder.deleteCharAt(AbstractStringBuilder.java:824)
	at java.lang.StringBuilder.deleteCharAt(StringBuilder.java:253)

3. Remove last char if it is equal to specific character

This example extends the previous one. We just add one more condition. We need to check if the last letter of StringBuilder is equals to the letter we want to find.

public class Example {

    public static void main(String[] args) {

        StringBuilder builder = new StringBuilder();

        builder.append("1234a");
        System.out.println( builder.toString() ); // 1234a

        removeLastChar(builder, 'a');
        System.out.println( builder.toString() ); // 1234
    }

    public static void removeLastChar(StringBuilder builder, char letter) {
        if (builder.length() > 0 && builder.charAt(builder.length() - 1) == letter) {
            builder.deleteCharAt(builder.length() - 1);
        }
    }
}

References

  1. StringBuilder - oracle docs
0 contributions

Checkout latest Findings & News:

Checkout latest questions:

Checkout latest wiki articles:

Hey 👋
Would you like to know what we do?
  • Dirask is IT community, where we share coding knowledge and help each other to solve coding problems.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.
Read more