public String key(String s, Object... o) { if (s == null || o != null && o.length > 0) { throw new IllegalArgumentException("Fingerprint keyer accepts a single string parameter"); } s = s.trim(); // first off, remove whitespace around the string s = s.toLowerCase(); // then lowercase it s = punctctrl.matcher(s).replaceAll(""); // then remove all punctuation and control chars String[] frags = StringUtils.split(s); // split by whitespace TreeSet<String> set = new TreeSet<>(); Collections.addAll(set, frags); StringBuilder b = new StringBuilder(); Iterator<String> i = set.iterator(); while (i.hasNext()) { // join ordered fragments back together b.append(i.next()); if (i.hasNext()) { b.append(' '); } } return asciify(b.toString()); // find ASCII equivalent to characters }