Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove appended strings to StringBuilder

How do I remove the previously appended strings to my String Builder? If I have for example

result.append("$10.00");
result.append("+" + "$20.00");
result.append("+" + "$5.00");

How do I remove $5 and $20 respectively from a string which is now "$10.00 + $20.00 + $5.00"?

like image 403
SleepNot Avatar asked Feb 19 '26 06:02

SleepNot


2 Answers

Once you append it, it becomes "one" and you cant reverse

However you can use this in this case :

result.delete(result.lastIndexOf("+"), result.length());
like image 155
libik Avatar answered Feb 20 '26 20:02

libik


There is no immediate way to do this. I suggest you append each substring to a List or even to a Stack, and pop whatever you don't need out of your data structure. When you are totally sure about what to present in your StringBuilder, start appending to it by running through your Collection and putting each substring into your final StringBuilder.

like image 37
Jorge_B Avatar answered Feb 20 '26 18:02

Jorge_B