Created
March 29, 2012 07:54
-
-
Save leon/2234719 to your computer and use it in GitHub Desktop.
Slugify
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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