Top community members
All Wiki Articles Create Wiki Article

Welcome to Dirask IT community! ❤ 💻
We are community of people that helps each other.

If you are beginner in IT field, you are more then welcome to ask questions, it will help you to learn faster. We are here to help you.

We are always beginner in something, we just need to remember it along the way.

there are no wrong questions - Ask Question

Java - remove last character from string

0 contributions
14 points

1. Overview

In this post we will see how to remove last character from String in java.

All below examples except StringUtils.chop can be used to remove any number of characters from the end of the string.

Example:

String original = "123";
String expected = "12";

2. Remove last char with String.substring

Code example: 

public class RemoveCharExample1 {

    public static void main(String[] args) {
        String original = "123";
        String result = original.substring(0, original.length() - 1);

        System.out.println(original); // 123
        System.out.println(result);   // 12
    }
}

Output:

123
12

3. Remove last char with StringBuilder.delete

Code example: 

public class RemoveCharExample2 {

    public static void main(String[] args) {
        String original = "123";

        StringBuilder builder = new StringBuilder(original);
        builder.delete(builder.length() - 1, builder.length());

        String result = builder.toString();

        System.out.println(original); // 123
        System.out.println(result);   // 12
    }
}

Output:

123
12

4. Remove last char with Java 8 Optional

Code example: 

import java.util.Optional;

public class RemoveCharExample3 {

    public static void main(String[] args) {
        String original = "123";

        String result = Optional.ofNullable(original)
                .map(str -> str.substring(0, str.length() - 1))
                .orElse(original);

        System.out.println(original); // 123
        System.out.println(result);   // 12
    }
}

Output:

123
12

5. Remove last char with Apache Commons - StringUtils.chop

Code example:

import org.apache.commons.lang3.StringUtils;

public class RemoveCharExample4 {

    public static void main(String[] args) {
        String original = "123";
        String result = StringUtils.chop(original);

        System.out.println(original); // 123
        System.out.println(result);   // 12
    }
}

Output:

123
12

6. Remove last char with Apache Commons - StringUtils.substring()

Code example: 

import org.apache.commons.lang3.StringUtils;

public class RemoveCharExample5 {

    public static void main(String[] args) {
        String original = "123";
        String result = StringUtils.substring(original, 0, original.length() - 1);

        System.out.println(original); // 1234
        System.out.println(result);   // 12
    }
}

Output:

123
12

References

  1. String.substring - Java docs
0 contributions

Suggested wiki articles:

Suggested findings

Let's do the work and have some fun
  • Dirask is online IT community for professionals and hobbyist to share their knowledge and help each other in extraordinary easy way. ❤ 💻 🙂
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    we will help you. 👍 ✔ 🦄
Read more