How do i remove all non url friendly characters in a string

I have a strng e.g. The cat sat on the mat

I'd like this to form a url page name e.g. The_cat_Sat_on_the_mat

But is there any easier and more accuracte way to ensure that "ALL" other non freidnly url can be removed.

By solution at the min is this ...

title.Replace(" ", "_").Replace(".", "").Replace(",", "").Replace("-", "").Replace("&", "").Replace("$", "")

But I'll be there all day saying < , > ?, | , ( , ) and so on. Is there a regular expression i can use which will remove only show 0-9 and a-z and remove all others?

And if possible could i get this in a working example.

Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

malikirfan28Connect With a Mentor Commented:
Note that regular expression will only find the matches and it can validate given string that either it is a valid URL or not.

For removing unnecessary characters from string, you can use given below simple logic. As you mentioned that valid URL will have only "a-z" or "A-z" or "0-9" so here is the C# function for it. You can alter it to add any other character.
private string ValidateURL(string value)
            string returnVal = "";
            char[] charArray = value.ToCharArray();
            for (int i = 0; i < charArray.Length; i++)
                int asciVal = (int)charArray[i];
                if ((asciVal >= 65 && asciVal <= 91) || (asciVal >= 97 && asciVal <= 123) || (asciVal >= 48 && asciVal <= 57) || asciVal == 32 || asciVal == 95)
                    returnVal = returnVal + charArray[i];
            return returnVal;

Open in new window

Guy Hengel [angelIII / a3]Billing EngineerCommented:
käµfm³d 👽Commented:

That sure is a lot of code just to sanitize a string  = )


I would have suggested this sooner, but I misread the question initially.
string value = "The cat sat on the mat";
string cleansed = System.Text.RegularExpressions.Regex.Replace(value, "[^\w]", "_");  // Any character NOT A-Z, a-z, 0-9, or _

Open in new window

All Courses

From novice to tech pro — start learning today.