How to remove the last character of a string in Java

How to remove the last character of a string in Java:

Strings are immutable in Java. We can’t change or modify a string. If you do any modification to a string, it will create another string. For example, if you are adding a character to a string, it will create one more string object.

The characters of a string can be accessed by its index. The index starts from 0 and ends at length of the string - 1. i.e. the index of the first character is 0, of the second character is 1 etc.

Removing the last character of a string is removing the string length - 1th character.

In this post, I will show you two different ways to remove the last character of a string in Java.

Method 1: By using substring:

This is the easiest way to remove the last string character. Java String class provides a method called substring, by using which we can get a substring from a string.

This method is defined as like below:

public String substring(int start, int end)

Where, start and end are the start index and end index for the substring.

  • It creates a sub-string from start(inclusive) to end(exclusive) and returns that sub-string.
  • end is optional. If we don’t provide its value, it will pick to the end of the string.

But, remember that it might throw StringIndexOutOfBoundsException if:

  • The value of start is negative
  • If end is less than start
  • If start and end are greater than the length of the string.

We can use this method to remove the last character of a string. We have to pass the value of start as 0 and end as length of the string - 1. Since the value of end is exclusive, it will return one string excluding the last character.

The below program shows how to do that:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        String str, str2;

        Scanner sc = new Scanner(System.in);

        System.out.println("Enter a string: ");
        str = sc.nextLine();

        str2 = str.substring(0, str.length() - 1);

        System.out.println("Original string: " + str);
        System.out.println("String removing last character: " + str2);
    }
}

In this program,

  • str and str2 are two String variables.
  • sc is a Scanner object to read the user input values.
  • It asks the user to enter a string. By using sc, it reads that string and put that in str.
  • It uses the substring method to find the required string. The start index is 0 and end index is length of the string - 1 passed to substring. It returns the new string by removing the last character and it is stored in str2.
  • The last two lines are used to print the original string and the newly created string.

If you run this program, it will print output as like below:

Enter a string: 
Hello World
Original string: Hello World
String removing last character: Hello Worl

Method 2: By converting the string to a StringBuilder object:

Another way to manipulate a string is by converting it to a StringBuilder object. StringBuilder has many advantages over strings. It doesn’t create any new object if we make any changes. We need to use the below method of StringBuilder:

public StringBuilder deleteCharAt(int i)

It takes one index value and delete the character at that index for the StringBuilder. We can call toString() method to convert a StringBuilder to string.

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        String str, str2;

        Scanner sc = new Scanner(System.in);

        System.out.println("Enter a string: ");
        str = sc.nextLine();

        str2 = new StringBuilder(str).deleteCharAt(str.length() - 1).toString();

        System.out.println("Original string: " + str);
        System.out.println("String removing last character: " + str2);
    }
}

Here, we are converting the string to a StringBuilder, deleting the last character and converting it back to a string.

It will give similar output:

Enter a string: 
Hello World
Original string: Hello World
String removing last character: Hello Worl

Java remove last string character

You might also like: