In this quick article, we'll look at different ways to remove the last character of a string in Java.
The easiest and quickest way to remove the last character from a string is by using the
String.substring() method. Here is an example:
String str = "Hey, there!!"; // remove last character (!) str = str.substring(0, str.length() -1); // print the new string System.out.println(str);
As you can see above, we are using
substring() with two parameters. The first parameter is the starting inclusive index which is 0 in our case. The second parameter is basically the ending exclusive index, calculated by taking the length of the string using the
length() method and by subtracting 1 from the length.
Now if you execute the code, you should see the following output:
However, there is a minor issue. The
substring() method is not null-safe, which means it will through an exception if the string is
null or empty. To avoid the exception, we have to explicitly check the length of the string as shown in the below example:
str = str != null && !str.isEmpty() ? str.substring(0, str.length() - 1) : null;
If you prefer to use Java 8 streams, just do the following:
str = Optional.ofNullable(str) .filter(s -> !s.isEmpty()) .map(s -> s.substring(0, s.length() - 1)) .orElse(str);
Using Regular Expression
Another way to remove the last character from a string is by using a regular expression with the
replaceAll() method. This method replaces all occurrences of the matched string with the given string.
replaceAll() method takes two input parameters: the regular expression and the replacement string. Here is an example code snippet that removes the last character of the string using
String str = "Hello World!"; // remove last character (!) str = str.replaceAll(".$", ""); // print the new string System.out.println(str);
replaceAll() is a part of the
String class, it is not null-safe. So we have to do a little more work:
str = str != null && !str.isEmpty() ? str.replaceAll(".$", "") : null;
replaceAll() method compiles the regular expression every time it executes. To avoid this unnecessary compilation, you should use the
Pattern class to compile the regular expression into a patternjava:
Pattern pattern = Pattern.compile(".$"); str = str != null && !str.isEmpty() ? pattern.matcher(str).replaceAll("") : null;
The equivalent example that uses Java 8 streams to remove the last character of the string:
str = Optional.ofNullable(str) .map(s -> s.replaceAll(".$", "")) .orElse(str);
Apache Commons Lang
The Apache Commons Lang library is a popular open-source library that provides many utility classes. One such class is the
StringUtils that offers useful utility methods for string operations.
To add the library to your Maven project, add the following dependency to
<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.9</version> </dependency>
For Gradle, add the below dependency to your
To remove the class character from a string, just use the
StringUtils.substring() method. This method is null-safe, which means it won't throw an exception if the string is
String str = "Hello World!"; // remove last character (!) StringUtils.substring(str, 0, str.length() - 1); // print the new string System.out.println(str);
StringUtils.substring() method takes one extra parameter than the built-in
substring(); the string you want to remove a character from.
Read Next: How to extract digits from a string in Java
✌️ Like this article? Follow me on Twitter and LinkedIn. You can also subscribe to RSS Feed.