replace will replace all instances of a letter. All you need to do is use substring():

public String method(String str) {
    if (str != null && str.length() > 0 && str.charAt(str.length() - 1) == 'x') {
        str = str.substring(0, str.length() - 1);
    }
    return str;
}
Answer from Gyan on Stack Overflow
🌐
Baeldung
baeldung.com › home › java › java string › how to remove the last character of a string?
How to Remove the Last Character of a String? | Baeldung
May 11, 2024 - 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); } 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.
Discussions

How to eliminate last character in a java string - Stack Overflow
I need to eliminate the last character of the string of 's' I have tried to use a variable to have it set to the last character and used that variable to put it in the parameters of the subscript ... More on stackoverflow.com
🌐 stackoverflow.com
Remove the last chars of the Java String variable - Stack Overflow
I want to remove the last four characters i.e., .null. Which method I can use in order to split. ... Man, the String class itself has all required methods. Just type path. and spend 2 minutes looking at the code completion options. More on stackoverflow.com
🌐 stackoverflow.com
Removing last 3 characters from a string? (SQLite)
Is there a way to just cut the last three digits off? SELECT SUBSTR(yourcolumn, 1, LENGTH(yourcolumn)-3 ) FROM ... More on reddit.com
🌐 r/SQL
6
6
April 10, 2023
removing all non-letter characters from a string? ((using regex))
This is very simple with regex. You just need to replace non-words (/\W/ig). So: var string = "lakjsdlkasjdlsaj@£$%^&*klajdlaskjds"; string.replace(/\W/ig, ""); --> "lakjsdlkasjdlsajklajdlaskjds" \w == words, \W == non words. You don't need a loop here as we're using /g which stands for global, so we replace every instance. And just incase I'm using /i as well which ignores the case. As it's regex you can just do /ig and the selectors will stack to ignore case and global. My favorite regex visualiser lives here: https://jex.im/regulex/#!embed=false&flags=ig&re=%5CW More on reddit.com
🌐 r/learnjavascript
10
4
September 28, 2015
🌐
GeeksforGeeks
geeksforgeeks.org › dsa › remove-first-and-last-character-of-a-string-in-java
Remove first and last character of a string in Java - GeeksforGeeks
After removing both the characters, the string will be "av". ... The idea is to use the String.substring() method of string class to remove the first and the last character of a string.
Published   July 15, 2025
🌐
How to do in Java
howtodoinjava.com › home › string › remove last character from a string in java
Remove Last Character from a String in Java
October 10, 2023 - The StringUtils.chop() returns a new string whose last character has been removed. This technique is noll-safe and handles empty strings as well. Also, note that if the string ends in \r\n, then it removes both of them.
🌐
Xenovation
xenovation.com › home › blog › java › ways how to remove last character from string in java
Ways how to remove last character from String in Java | XENOVATION
June 7, 2020 - Java's built-in method substring() of the class String is the most known way of how to remove the last character. This is done by getting from the existing String all characters starting from the first index position 0 till the next to last ...
🌐
Javatpoint
javatpoint.com › how-to-remove-last-character-from-string-in-java
How to Remove Last Character from String in Java
How to Remove Last Character from String in Java with java tutorial, features, history, variables, object, class, programs, operators, oops concept, array, string, map, math, methods, examples etc.
🌐
DigitalOcean
digitalocean.com › community › tutorials › java-remove-character-string
Java String Replace: How to Replace Characters and Substrings | DigitalOcean
February 20, 2025 - Both methods are effective for replacing multiple substrings in a Java string. The choice between them depends on the complexity of the replacements and personal preference. In this article, you learned various ways to remove characters from strings in Java using methods from the String class, including replace(), replaceAll(), replaceFirst(), and substring().
Find elsewhere
🌐
Baeldung
baeldung.com › home › java › java string › remove the last character of a java stringbuilder
Remove the Last Character of a Java StringBuilder | Baeldung
January 8, 2024 - We can use StringBuilder‘s substring() method to get a subsequence from the given start and end indexes of the string. The method requires two arguments, the beginning index (inclusive) and the ending index (exclusive). It’s worth mentioning that the substring() method returns a new String object. In other words, the substring() method doesn’t modify the StringBuilder object. We can pass 0 as the start index and the last character’s index as the end index to the substring() method to get a string with the last character chopped off:
🌐
Blogger
javarevisited.blogspot.com › 2016 › 03 › how-to-remove-first-and-last-character-from-String-in-java-example.html
How to Remove First and Last Character of String in Java - Example Tutorial
Here are some important points about the substring method in Java for your quick reference: Another way to take out the first and last character from String is by using StringBuffer and StringBuilder class.
🌐
TutorialsPoint
tutorialspoint.com › article › How-to-remove-the-last-character-from-a-string-in-Java
How to remove the last character from a string in Java?
The StringBuffer class contains a method known as deleteCharAt(). This method deletes the character at a specified index/position. You can use this method to delete/remove a particular character from a string in Java.
🌐
Coderanch
coderanch.com › t › 432786 › java › Remove-character-String
Remove last character from String (Beginning Java forum at Coderanch)
February 24, 2009 - So i want to store it like Oven,Tray in the String. while(rs3.next()) { Items += rs3.getString(2); } where do i add the "," character? I hope you got my Query. Thanks. ... That is completely different from what you asked earlier. Don't use String concatenation. Use a StringBuilder and its append method. Much better performance. The only problem is that you need to add a comma after all but the last entries.
🌐
Attacomsian
attacomsian.com › blog › java-remove-last-character-of-string
Remove the last character of a string in Java
February 22, 2020 - 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.
🌐
Sabe
sabe.io › blog › java-remove-last-character-from-string
How to Remove the Last Character from a String in Java | Sabe
June 18, 2022 - To remove the last character of a string, let's first start with our example string: ... Now, we can use the substring method which takes a start index and an end index and returns a substring of the original string.
🌐
javaspring
javaspring.net › blog › java-remove-last-character-from-string
Java: Removing the Last Character from a String — javaspring.net
In this code, the substring(0, original.length() - 1) method call creates a new string that starts from the beginning of the original string (index 0) and ends at the second-last character (index original.length() - 1). StringBuilder and StringBuffer are mutable sequences of characters in Java. You can convert a string to a StringBuilder or StringBuffer, remove the last character, and then convert it back to a string.
🌐
W3Docs
w3docs.com › java
Remove last character of a StringBuilder? | W3Docs
To remove the last character of a StringBuilder in Java, you can use the deleteCharAt method and pass in the index of the character you want to delete.
🌐
Python Examples
pythonexamples.org › java › how-to-remove-last-n-characters-in-string
How to Remove last N Characters in a String in Java
// Removing Last 3 Characters from "Hello World" String public class Main { public static void main(String[] args) { String str1 = "Hello World"; String modifiedStr = str1.substring(0, str1.length() - 3); System.out.println("Modified string is: " + modifiedStr); } }
🌐
JavaBeat
javabeat.net › home › how to remove character from a string in java
How to Remove Character From a String in Java
April 2, 2024 - The replace() and substring() methods have several variants that can be used (according to the requirements) to remove characters from a string. Different examples of all these methods are discussed in this Java post to remove a character from a string.
🌐
Edureka Community
edureka.co › home › community › categories › java › how to remove the last character from a string
How to remove the last character from a string | Edureka Community
December 30, 2020 - 101597/how-to-remove-the-last-character-from-a-string ... I have created a code in java for a flower shop and now my IDE is showing all the inputs are in red showing there is an error Jul 6, 2024