Skip to content

Instantly share code, notes, and snippets.

@leon
Created March 29, 2012 07:54
Show Gist options
  • Save leon/2234719 to your computer and use it in GitHub Desktop.
Save leon/2234719 to your computer and use it in GitHub Desktop.
Slugify
public class Slugify {
public static String slugify(String input) throws UnsupportedEncodingException {
if (input == null || input.length() == 0) return "";
String toReturn = normalize(input);
toReturn = toReturn.replace(" ", "-");
toReturn = toReturn.toLowerCase();
toReturn = URLEncoder.encode(toReturn, "UTF-8");
return toReturn;
}
private static String normalize(String input) {
if (input == null || input.length() == 0) return "";
return Normalizer.normalize(input, Form.NFD).replaceAll("[^\\p{ASCII}]","");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment