Best way to create SEO friendly URI string
- by Mat Banik
The method below allows only "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-" chars in URI strings.
What better way is there to make nice SEO URI string?
import org.apache.commons.lang.StringUtils;
public static String safeChar(String input) {
input = input.trim();
input = StringUtils.replace(input, " -", "-");
input = StringUtils.replace(input, "- ", "-");
input = StringUtils.replace(input, " - ", "-");
input = StringUtils.replaceChars(input, '\'', '-');
input = StringUtils.replaceChars(input, ' ', '-');
char[] allowed = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-".toCharArray();
char[] charArray = input.toCharArray();
StringBuilder result = new StringBuilder();
for (char c : charArray) {
for (char a : allowed) {
if (c == a) result.append(a);
}
}
return result.toString();
}