Replacing The Extended ASCII Dash in C# and SQL

Not all dashes are created equal. That’s what I learned today. If you look at the ASCII Character Codes CheatSheet you will see 3 different dashes. The first is the normal one. The other two are considered extended ASCII. Extended ASCII is a polite way of saying it doesn’t appear on your keyboard.

45  -
150 –
151 —

Over on DeepFitness.com, I try to create a friendly URL to each article. When you perform an HttpUtility.UrlEncode against a regular dash, it returns a dash. When you perform it against the 2nd dash it returns %e2%80%93. The 3rd dash will return %e2%80%94. Not exactly a search engine friendly URL.

I’m sure there are 10 ways to replace the bad dash with the good dash in C#. Here is the method I used.

string titleLink;
// assign titleLink a value - database perhaps
titleLink = HttpUtility.UrlEncode(titleLink);
// remove Extended ASCII dash with ASCII dash
titleLink = titleLink.Replace("%e2%80%93", "-");

A better way is to clean it up at the database level. Here is the SQL that will replace the extended dash with the normal dash.

UPDATE Article
SET title = REPLACE(title,CHAR(150),CHAR(45))
This entry was posted in C#, SQL and tagged . Bookmark the permalink.

Comments are closed.