3

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

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
fasheikh
  • 419
  • 3
  • 19

4 Answers4

3

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
Bharat Sinha
  • 13,973
  • 6
  • 39
  • 63
  • 1
    nice thought. but really this wants to be a map or set of some sort. then the string is a toString function return value. – Randy Sep 10 '12 at 14:07
  • Rather than building the string yourself, I would use a String/Collection utility library (Guava's Joiner or Commons StringUtils) which take a similar approach but don't duplicate the String object with use of substring and are flexible in the separator that is used (read: higher reuse) – Tom Hartwell Jul 03 '14 at 16:48
1

I would rebuilt the string each time, only including the element you want to include.

e.g.

Set<Planet> planets = ...
StringBuilder sb = new StringBuilder();
String sep = "";
for(Planet planet: planets) {
   sb.append(sep).append(planet.getDisplayName());
   sep = ",";
}
String planetNames = sb.toString();
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • 2
    Nice approach. Apache Commons StringUtils is flexible and handles this nicely, as does Guava's Joiner library. I would lean towards using those in a project of any size. For example, StringUtils optimizes the StringBuilder initial size and handles nulls gracefully. – Tom Hartwell Jul 03 '14 at 16:52
0

I think you're using StringBuffer or something like that. It's not a good solution it this case, I would recommend you to use Set. http://docs.oracle.com/javase/6/docs/api/java/util/Set.html

Implement it like this:

Set planets = new HashSet();
planets.add("a");
planets.add("b");
planets.remove("a");
user1049280
  • 5,176
  • 8
  • 35
  • 52
0

Assume... StringBuffer savedPlanets...

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

OPPOSITE OF APPEND : savedPlanets.replaceAll(planet.getDisplayName()+",","");

EX:

savedPlanets.append("Earth,");
savedPlanets.append("Mercury,");
savedPlanets.append("Venus,");

now savedPlanetes= "Earth,Mercury,Venus,";

then

savedPlanets.replaceAll("Venus,",""); <-----

now value will be savedPlanetes= "Earth,Mercury,";

MAC
  • 15,799
  • 8
  • 54
  • 95