Skip to content

Instantly share code, notes, and snippets.

@ricardotealdi
Created October 10, 2011 21:50
Show Gist options
  • Save ricardotealdi/1276662 to your computer and use it in GitHub Desktop.
Save ricardotealdi/1276662 to your computer and use it in GitHub Desktop.
Hyphenator
package br.com.tealdi.testing;
import java.text.Normalizer;
public class Hyphenator {
public String hyphenizeIt(String text) {
return trimmingHyphens(
removeNonCharactersButHyphen(
convertSpacesIntoHyphens(
removeAccents(
text.toLowerCase()))));
}
private static String trimmingHyphens(String text) {
return text.replaceAll("^\\W|\\W$", "");
}
private static String removeAccents(String text) {
return Normalizer
.normalize(text, Normalizer.Form.NFD)
.replaceAll("\\p{InCombiningDiacriticalMarks}+", "");
}
private static String removeNonCharactersButHyphen(String text) {
return text.replaceAll("[^\\w|-]", "");
}
private static String convertSpacesIntoHyphens(String text) {
return text
.trim()
.replaceAll("[\\s]+", "-");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment