Strip certain characters from a String

From CodeCodex

Implementations[edit]

Java[edit]

This example keeps only a given set of accepted characters.

 public class StringUtils {
  public static void main(String args[]) {
     System.out.println
       (StringUtils.stripGarbage("A good String"));
     System.out.println
       (StringUtils.stripGarbage("String with !%garbage &*("));
     /*
     output :
        A good String
        String with garbage
     */
     }

  public static String stripGarbage(String s) {
    String good =
      " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    String result = "";
    for ( int i = 0; i < s.length(); i++ ) {
        if ( good.indexOf(s.charAt(i)) >= 0 )
           result += s.charAt(i);
        }
    return result;
    }
}

NOTE: You may want to look at How-to optimize string operations

The following snippet will strip or keep from a given string the specified characters.

Thanks to T. GUIRADO for the idea

 public class StringUtils {
  /**
   * @param s   source string
   * @param toMatch  target character(s)  
   * @param boolean  true=keep  false=strip
   **/
   public static String cleanUp
        ( String s, String sToMatch, boolean isToKeep ) {
     final int size = s.length();
     StringBuffer buf = new StringBuffer( size );
     if ( ! isToKeep ) {
       for ( int i = 0; i < size; i++ ){
         if ( sToMatch.indexOf(s.charAt(i) ) == -1 ){
           buf.append( s.charAt(i) );
         }
       }
     }
     else {
       for ( int i = 0; i < size; i++ ){
         if ( sToMatch.indexOf(s.charAt(i) ) != -1 ){
           buf.append( s.charAt(i) );
         }
       }
     }
     return buf.toString();
   } 
   
   public static void main(String args[]) {
     System.out.println(cleanUp("realhowGARBhowtoAGE", "GARBAGE", false));
     System.out.println(cleanUp("THISrealIShowtoGOOD", "THISISGOOD", true));
     /*
      * output :
      *  realhowhowto
      *  THISISGOOD
      */
   }
   
}

Ruby[edit]

def strip_garbage(s)
  good = " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
  result = ""
  s.each_byte do |byte|
    result << byte  if good.include?(byte.chr)
  end
  result
end

p strip_garbage("A good String")
p strip_garbage("String with !%garbage &*(")
p "String with !%garbage &*(".delete("^ a-zA-Z0-9")     # used builtin method

def clean_up(s, match, keep)
  unless keep
    match = '\\' + match  if match[0,1]=="^"
    s.delete(match)
  else
    s.delete("^" + match)
  end
end

p clean_up("realhowGARBhowtoAGE", "GARBAGE", false)
p clean_up("THISrealIShowtoGOOD", "THISISGOOD", true)

p "realhowGARBhowtoAGE".delete("GARBAGE")       # It is the same above.
p "THISrealIShowtoGOOD".delete("^THISISGOOD")   #