# Remove the last character of a string in Java

By Atta Ur Rehman Shah (https://attacomsian.com/about). Published Feb 22, 2020. Topics: Java.
Canonical URL: https://attacomsian.com/blog/java-remove-last-character-of-string

> Learn about different ways to remove the last character of a string in Java.

In this quick article, we'll look at different ways to remove the last character of a string in Java.

## Using `String.substring()`

The easiest and quickest way to remove the last character from a string is by using the `String.substring()` method. Here is an example:

```java
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:

```bash
Hey, there!
```

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:

```java
str = str != null && !str.isEmpty() ? str.substring(0, str.length() - 1) : null;
```

If you prefer to use **Java 8 streams**, just do the following:

```java
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. 

The `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 `replaceAll()`:

```java
String str = "Hello World!";

// remove last character (!)
str = str.replaceAll(".$", "");

// print the new string
System.out.println(str);
```

Since `replaceAll()` is a part of the `String` class, it is **not null-safe**. So we have to do a little more work:

```java
str = str != null && !str.isEmpty() ? str.replaceAll(".$", "") : null;
```

The `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:

```java
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:

```java
str = Optional.ofNullable(str)
        .map(s -> s.replaceAll(".$", ""))
        .orElse(str);
```

## Apache Commons Lang

The [Apache Commons Lang](https://commons.apache.org/proper/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 `pom.xml` file:

```xml
<dependency>
  <groupId>org.apache.commons</groupId>
  <artifactId>commons-lang3</artifactId>
  <version>3.9</version>
</dependency>
```

For Gradle, add the below dependency to your `build.gradle` file:

```groovy
implementation 'org.apache.commons:commons-lang3:3.9'
```

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 `null`:

```java
String str = "Hello World!";

// remove last character (!)
StringUtils.substring(str, 0, str.length() - 1);

// print the new string
System.out.println(str);
```

The `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](https://attacomsian.com/blog/java-extract-digits-from-string)
