Java StringBuilder remove last character example (StringBuffer)

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

publicStringBuilder deleteCharAt(intindex)

This method removes a character 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

publicclassStringBuilderRemoveLastCharacterExample{

publicstaticvoidmain(String[]args){

//creates an empty StringBuilder object

StringBuilder sbld=newStringBuilder();

int[]numbers={1,2,3,4,5};

for(intnumber: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

publicvoidsetLength(intnewLength)

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

publicclassStringBuilderRemoveLastCharacterExample{

publicstaticvoidmain(String[]args){

//creates an empty StringBuilder object

StringBuilder sbld=newStringBuilder();

int[]numbers={1,2,3,4,5};

for(intnumber: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

publicAbstractStringBuilder deleteCharAt(intindex){

if((index<0)||(index>=count))

thrownewStringIndexOutOfBoundsException(index);

System.arraycopy(value,index+1,value,index,count-index-1);

count--;

returnthis;

}

And here is the source code of the setLength method.

1

2

3

4

5

6

7

8

9

10

11

publicvoidsetLength(intnewLength){

if(newLength<0)

thrownewStringIndexOutOfBoundsException(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.

About the author

RahimV

My name is RahimV and I have over 16 years of experience in designing and developing Java applications. Over the years I have worked with many fortune 500 companies as an eCommerce Architect. My goal is to provide high quality but simple to understand Java tutorials and examples for free. If you like my website, follow me on Facebook and Twitter.