Best way to create SEO friendly URI string

Posted by Mat Banik on Stack Overflow See other posts from Stack Overflow or by Mat Banik
Published on 2011-01-02T23:10:22Z Indexed on 2011/01/03 1:53 UTC
Read the original article Hit count: 544

Filed under:
|
|
|
|

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();
}

© Stack Overflow or respective owner

Related posts about java

Related posts about string