How to Remove Last Character from String in Java
In Java, there are mainly three classes related to the String. The classes are String, StringBuilder, and StringBuffer class that provides methods related to string manipulation. Removing the first and last character from the String is also an operation that we can perform on the string.
In this section, we will learn how to remove the last character from String in Java. At the last of this section, we have also explained how to delete the first and last character of each word in a string.
There are four ways to remove the last character from a string:
- Using StringBuffer.deleteCahrAt() Class
- Using String.substring() Method
- Using StringUtils.chop() Method
- Using Regular Expression
Using StringBuffer Class
The StringBuffer class provides a method deleteCharAt(). The method deletes a character from the specified position. We use the method to remove a character from a string in Java. It accepts a parameter index of type int. The index is the position of a character we want to delete. It returns this object.
It throws StringIndexOutOfBoundsException if we specify a negative index or the index is greater than or equal to the length of the string.
Let’s implement the method in an example.
Tutoraspire is the best educational website
In the above output, we see that the last character s has been deleted.
Using String.substring() Method
The substring() is the method of the String class. It parses two parameters beginIndex and endIndex of type int. It returns a new string (sub-string). It is not thread-safe because does not throws an exception if the string is null or empty.
If the beginIndex is negative or beginIndex > endIndex or the endIndex > length of the string it throws IndexOutOfBoundsException.
Welcome to TutorAspire
Using StringUtils.chop() Method
The StringUtils class provides a chop() method to remove the last character from a string. The method parses a parameter of type String. It also accepts null, as a parameter. It returns the string after removing the last character. It also returns a null string when we input a null string.
For using the chop() method of the StringUtils class, we need to add the following dependency in the pom.xml file. When we add the Apache commons lang3 jar in the pom file it downloads the jar file and add the jar file to the path. We must import the package
After adding the dependency, we can call the chop() method of StringUtils class to remove the last character from the string.
Using Regular Expression
We can also use the regular expression to remove or delete the last character from the string. The String class provides the replaceAll() method that parses two parameters regex and replacement of type String. The method replaces the string with the specified match.
- regex: It is the expression with which the string is to match.
- replacement: It is the replacement string or substitute string for each match.
It returns the resulting substring.
It throws PatternSyntaxException if the regular expression syntax is invalid.
Honesty is the best polic
Removing the First and Last Character of Each Word in a String
We can also remove or delete the first and last character of each word in a string. To remove the first and last character, we use the following steps:
- Split (break) the String based on the space.
- For each word, run a loop form the first to last letter.
- Identify the first and last character of each word.
- Now, delete the first and last character of each word.
Tutoraspire is the best educational website avatpoin h es ducationa ebsit
In the above output, we see that the first and last character has been removed from each word of the string. The word “is” has been removed completely because it has only two characters.