Expand Authors Top

If you have a few years of experience in the Java ecosystem and you’d like to share that with the community, have a look at our Contribution Guidelines.

Partner – Frontegg – expanded (cat = Security)
announcement - icon User management is very complex, when implemented properly. No surprise here.

Not having to roll all of that out manually, but instead integrating a mature, fully-fledged solution - yeah, that makes a lot of sense.
That's basically what Frontegg is - User Management for your application. It's focused on making your app scalable, secure and enjoyable for your users.
From signup to authentication, it supports simple scenarios all the way to complex and custom application logic.

Have a look:

>> Elegant User Management, Tailor-made for B2B SaaS

Partner – Frontegg – expanded (cat = Spring Security)
announcement - icon User management is very complex, when implemented properly. No surprise here.

Not having to roll all of that out manually, but instead integrating a mature, fully-fledged solution - yeah, that makes a lot of sense.
That's basically what Frontegg is - User Management for your application. It's focused on making your app scalable, secure and enjoyable for your users.
From signup to authentication, it supports simple scenarios all the way to complex and custom application logic.

Have a look:

>> Elegant User Management, Tailor-made for B2B SaaS

Java Top

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

> CHECK OUT THE COURSE

1. Overview

In this quick tutorial, we're going to explore different techniques for removing the last character of a String.

2. Using String.substring()

The easiest way is to use the built-in substring() method of the String class.

In order to remove the last character of a given String, we have to use two parameters: 0 as the starting index, and the index of the penultimate character. We can achieve that by calling String‘s length() method, and subtracting 1 from the result.

However, this method isn't null-safe, and if we use an empty string, this is going to fail.

To overcome issues with null and empty strings, we can wrap the method in a helper class:

public static String removeLastChar(String s) {
    return (s == null || s.length() == 0)
      ? null 
      : (s.substring(0, s.length() - 1));
}

We can refactor the code, and use Java 8:

public static String removeLastCharOptional(String s) {
    return Optional.ofNullable(s)
      .filter(str -> str.length() != 0)
      .map(str -> str.substring(0, str.length() - 1))
      .orElse(s);
    }

3. Using StringUtils.substring()

Instead of reinventing the wheel, we can use the StringUtils class from the Apache Commons Lang3 library, which offers helpful String operations. One of them is a null-safe substring() method, which handles exceptions.

To include StringUtils, we have to update our pom.xml file:

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.12.0</version>
</dependency>

StringUtils.substring() requires three parameters: a given String, an index of the first character (in our case it will be 0 always), and the index of the penultimate character. Again, we can simply use the length() method and subtract 1:

String TEST_STRING = "abcdef";

StringUtils.substring(TEST_STRING, 0, TEST_STRING.length() - 1);

Yet again, this operation isn't null-safe. It'll work fine with empty Strings though.

4. Using StringUtils.chop()

The StringUtils class provides the chop() method, which works well with all edge scenarios: empty and null Strings.

It's very easy to use, and requires only one parameter: the String. Its sole purpose is to remove the last character, nothing more, nothing less:

StringUtils.chop(TEST_STRING);

5. Using Regular Expression

We can also remove the last character (or any number of characters) from a String by making good use of regular expressions.

For example, we can use the replaceAll() method of the String class itself, which takes two parameters: the regular expression and the replacement String:

TEST_STRING.replaceAll(".$", "");

Note that, because we're calling a method on the String, the operation isn't null-safe.

Also, replaceAll() and regex expressions can be complex at first sight. We can read more about regex here, but to make the logic a bit more user-friendly, we can wrap it in a helper class:

public static String removeLastCharRegex(String s) {
    return (s == null) ? null : s.replaceAll(".$", "");
}

Note that if a String ends with a newline, then the above method will fail as “.” in regex matches for any character except for line terminators.

Finally, let's re-write the implementation with Java 8:

public static String removeLastCharRegexOptional(String s) {
    return Optional.ofNullable(s)
      .map(str -> str.replaceAll(".$", ""))
      .orElse(s);
}

6. Conclusion

In this brief article, we discussed different ways of removing only the last character of a String, some manual and some ready out of the box.

If we need more flexibility, and we need to remove more characters, we can use the more advanced solution with regular expressions.

As always, the code used throughout the article can be found over on GitHub.

Java bottom

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

>> CHECK OUT THE COURSE
Generic footer banner
Comments are closed on this article!