How to do the opposite of StringBuilder append in Java?

What would be the opposite of:

savedPlanets.append(planet.getDisplayName()+",");

I have a list and I am adding the name of the planet every time the user clicks on a checkbox, I want to remove the name from the savedPlanets if the checkbox is cleared

Answers


You can do following:

 savedPlanets.replace(planet.getDisplayName(),"");

Ideally I would do a solution like following:

Set<String> selectedPlanets = new HashSet<String>();

selectedPlanets.add(planet.getDisplayName());        // Whenever a planet is selected
selectedPlanets.remove(planet.getDisplayName());     // Whenever a planet is removed 

// Prepare a String with all planets        
StringBuilder savedPlanets = new StringBuilder("");
for(String planetName : selectedPlanets ){
    savedPlanets.append(planetName).append(",");
}
// Removing , from the end if any       
if(savedPlanets.toString().endsWith(","))
    finalValue = savedPlanets.substring(0, savedPlanets.length()-1);

// finalValue is what you are looking for

Need Your Help

Logical container in WPF to bind data

wpf data-binding controls

I want to bind one data to several controls. Is there some logical control in WPF to achieve this?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.