Remove last character from string in java
In this article, we will take a look at 7 different ways to remove last character from a string in java.
These include java inbuilt string class methods and from Apache Commons Lang library.
Java string class has
substring()
method which returns a portion of a string.It takes following 2 integer arguments
- start index from where substring will begin.
- end index till which substring will be done.
String returned by substring()
contains characters between start index and (end index – 1).
Remember, characters in a string have a numeric index, with the index of first character 0, second character 1 and so on.
So, to remove the last character, we need to get substring from index 0 till the second last character. Second last character will have an index equal to (length of the string – 1).
Java program to remove the last character from a string using substring()
method will be
String s = "learnings"; s = s.substring(0, s.length() - 1); System.out.println(s);
substring()
will return a string will last character just before the supplied end index, which, in this case is the index of last character.
Therefore, last character will be removed from the string.
Method 2: Using StringBuffer deleteCharAt()
Java StringBuffer class has a deleteCharAt()
method which takes an integer argument representing the index of character to be deleted.
To remove the last character from string, we need to supply (length of string – 1). Example,
String s = "learnings"; s = new StringBuffer(s).deleteCharAt(s.length() - 1).toString();
Method 3: Using java 8
Java 8 Optional can be used to remove last character from a string.
Optional
has a map()
method which transforms the value of string contained by Optional
object according to the function supplied to it.
Thus, to remove last character from a string, we supply a function that performs a substring()
from beginning till one less than the last character, similar as one given outlined in first method. Example,
String s = "learnings"; // create an optional Optional<String> op = Optional.ofNullable(s); // remove last character s = op.map(str -> str.substring(0, str.length() - 1)). orElse(s);
To create an Optional
object, use its static ofNullable()
method, which returns an empty Optional
, when the string is null
.
If you are sure that the string will never be null
, then of()
method can also be used.
map()
takes an argument of type java.util.Function
, which is a functional interface. This functional interface contains a single method which accepts one argument and returns one argument.
Thus, it can be represented as a Lambda expression, given as str -> str.substring(0, str.length() - 1)
.
Note that the lambda expression accepts and returns one argument.
Above code can be written as a one-liner below
String s = "learnings"; s = Optional.ofNullable(s). map(str -> str.substring(0, str.length() - 1)). orElse(s);
Method 4: Using StringUtils.substring()
This method makes use of StringUtils
class from Apache Commons Lang 3 library.
It has a substring method which takes 3 arguments
- Source string
- Start index
- End index
and returns a string between the given index positions. Character at the end index is not included in the string.
Indexing begins at 0. Example,
String s = "learnings"; s = StringUtils.substring(s, 0, s.length()-1);
Advantage of this method is that it will not give an exception when string is null
or the supplied index values are invalid, unlike inbuilt java methods explained above.
Method 5: Using StringUtils.chop()
If you know the last character of the string, then this method can be used to remove it.
chop()
also belongs to StringUtils
class from Apache Commons Lang 3.
It takes a single string argument and returns a new string with last character removed. Example,
String s = "learnings"; s = StringUtils.chop(s);
- If the supplied string is null, then
chop()
returnsnull
. - If the supplied string is empty, then
chop()
returns an empty string. - If the string ends with
\n
, then it will remove the newline character. That is,chop("xyz\n")
will return “xyz”.
removeEnd()
is another method from StringUtils
of Apache Commons Lang 3. It takes 2 arguments
- Source string.
- String to be removed
If string given as second argument lies at the end of source string, then it is removed.
So, if we provide a single character string as second argument, then this method will remove last character from the string. Example,
String s = "learnings"; s = StringUtils.removeEnd(s, "s");
- If the source string is
null
, it returnsnull
. - If the source string is empty, it returns empty string.
- If the string to be replaced is
null
or empty, it returns the source string unchanged.
Method 7: Using regular expression
Java string class has a replaceAll() method which takes two arguments :
- A regular expression
- A string
replaceAll()
matches the regular expression and replaces the characters matching this expression with the string supplied as second argument.
Regular expression to match the last character of a string is .$. Thus, replacing this regular expression with an empty string will remove the last character from a string. Example,
String s = "learnings"; s = s.replaceAll(".$", "");
Hope the article was useful.