Java tutorial
//package com.java2s; //License from project: Open Source License public class Main { private static String cleanupSearchString(String search) { if (search == null) return ""; StringBuilder out = new StringBuilder(search.length()); char prev = ' '; for (char curr : search.toCharArray()) { if (Character.isLetterOrDigit(curr)) { out.append(curr); } else { curr = ' '; if (!Character.isWhitespace(prev)) { out.append(curr); } } prev = curr; } return out.toString(); } }