How to Remove Last Character from String in JavaIn 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 ClassThe 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. Syntax: 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. RemoveLastCharcter1.java Output: Javatpoint is the best educational website In the above output, we see that the last character s has been deleted. Using String.substring() MethodThe 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. Syntax: If the beginIndex is negative or beginIndex > endIndex or the endIndex > length of the string it throws IndexOutOfBoundsException. RemoveLastCharacter2.java Output: Welcome to Javatpoin Using StringUtils.chop() MethodThe 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. Syntax: 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. RemoveLastCharacter3.java Output: Googl Using Regular ExpressionWe 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.
It returns the resulting substring. Syntax: It throws PatternSyntaxException if the regular expression syntax is invalid. RemoveLastCharacter4.java Output: Honesty is the best polic Removing the First and Last Character of Each Word in a StringWe 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:
RemoveFirstAndLastCharacter.java Output: Javatpoint 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. Next TopicHow TreeMap Works Internally in Java |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India