This example shows how to remove the last character from the StringBuilder or StringBuffer object using the deleteCharAt and setLength methods. It also shows the best way to delete the last character.
How to remove the last character from the StringBuilder or StringBuffer?
1. Using the deleteCharAt method
We can use the deleteCharAt
method to remove the character at any given index from the StringBuilder or StringBuffer object.
1 |
public StringBuilder deleteCharAt(int index) |
This method removes a character located at the specified index. To remove the last character, we need to pass StringBuilder length – 1 as the index to the deleteCharAt
method, as shown in the below example.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
public class StringBuilderRemoveLastCharacterExample { public static void main(String[] args) { //creates an empty StringBuilder object StringBuilder sbld = new StringBuilder(); int[] numbers = {1, 2, 3, 4, 5}; for(int number : numbers){ sbld.append(number).append(","); } System.out.println("StringBuilder contains: " + sbld); /* * To remove last character from StringBuilder, use * the deleteCharAt method */ //always make sure to check the length to avoid exception if( sbld.length() > 0 ) sbld.deleteCharAt( sbld.length() - 1 ); System.out.println("StringBuilder contains: " + sbld); } } |
Output
1 2 |
StringBuilder contains: 1,2,3,4,5, StringBuilder contains: 1,2,3,4,5 |
Before calling the deleteCharAt
method always check that the length of the StringBuilder object is greater than 0 to avoid the StringIndexOutOfBoundsException exception.
2. Using the setLength method
We can also use the setLength
method of the StringBuilder class to remove the last character from it.
1 |
public void setLength(int newLength) |
This method sets the length of the character sequence stored in the StringBuilder or StringBuffer object. To remove the last character, we need to pass StringBuilder length – 1 to the setLength
method as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
public class StringBuilderRemoveLastCharacterExample { public static void main(String[] args) { //creates an empty StringBuilder object StringBuilder sbld = new StringBuilder(); int[] numbers = {1, 2, 3, 4, 5}; for(int number : numbers){ sbld.append(number).append(","); } System.out.println("StringBuilder contains: " + sbld); /* * To remove last character from StringBuilder, use * the setLength method */ //always make sure to check the length to avoid exception if( sbld.length() > 0 ) sbld.setLength( sbld.length() - 1 ); System.out.println("StringBuilder contains: " + sbld); } } |
Output
1 2 |
StringBuilder contains: 1,2,3,4,5, StringBuilder contains: 1,2,3,4,5 |
What is the best way to remove the last character from StringBuilder or StringBuffer?
Let’s have a look at the source code for both of these methods. Here is the source code of the deleteCharAt
method which is directly taken from the OpenJDK 8 AbstractStringBuilder class.
1 2 3 4 5 6 7 |
public AbstractStringBuilder deleteCharAt(int index) { if ((index < 0) || (index >= count)) throw new StringIndexOutOfBoundsException(index); System.arraycopy(value, index+1, value, index, count-index-1); count--; return this; } |
And here is the source code of the setLength
method.
1 2 3 4 5 6 7 8 9 10 11 |
public void setLength(int newLength) { if (newLength < 0) throw new StringIndexOutOfBoundsException(newLength); ensureCapacityInternal(newLength); if (count < newLength) { Arrays.fill(value, count, newLength, '\0'); } count = newLength; } |
By looking at the source code above, we can see that the deleteCharAt
method uses the arraycopy
method of the System class to remove the specified character from the StringBuilder object. Whereas, the setLength
method simply sets the internal buffer count to the specified new length if the current length is greater than or equal to the new length (that is the case when we want to remove a character).
To copy an array is a costly operation as compared to setting the value of an internal variable in terms of performance. Hence, the setLength
method should be used to remove the last character over deleteCharAt
method due to performance reasons.
This example is a part of the Java StringBuffer tutorial and Java StringBuilder tutorial.
Please let me know your views in the comments section below.