Argentini.Halide.H3Text.UrlRewriteText C# (CSharp) Method

UrlRewriteText() public static method

Creates a URL-compatible string from plain text. Ideal for use with URL rewriting when you want to link to an article based on its headline text, and not a cryptic path like "pagename.aspx?ID=1234". Replaces any number of sequential non-alphanumeric characters with a single hyphen character. The returned string will have all leading and trailing hyphens stripped as well.

For example, "(Now is the time, I swear.)" becomes "Now-is-the-time-I-swear"

public static UrlRewriteText ( string input ) : String
input string The string to process (usually a subject or headline).
return String
        public static String UrlRewriteText(string input)
        {
            string final = input.Trim();

            final = final.Replace("'", "");

            Regex stripStuff = new Regex("([^a-zA-Z0-9])");
            final = stripStuff.Replace(final, "-");

            stripStuff = new Regex("_{1,}");
            final = stripStuff.Replace(final, "-");

            stripStuff = new Regex("_$");
            final = stripStuff.Replace(final, "");

            stripStuff = new Regex("^_");
            final = stripStuff.Replace(final, "");

            while (final.IndexOf("--") > 0)
            {
                final = final.Replace("--", "-");
            }

            if (final.EndsWith("-"))
            {
                final = final.Remove(final.Length - 1);
            }

            return (final);
        }