The following code snippet shows how to strip certain characters from a String. The main idea is to keep a list of allowed characters (good String variable in the example below) and remove the characters that are not in that list by creating another list from only the allowed characters.

 public class testGarbage {

  public static void main(String args[]) {
     testBeep t = new testGarbage();
     }

  public testGarbage() {   
     System.out.println(stripGarbage("A good String"));
     System.out.println(stripGarbage("String with !%garbage &*("));
     }

  public 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;
    }
}