How can i generate a unique string from the post title in my C# code ? Similar to the one which appears in the url of this post.
The string doesn't need to be unique, actually : if you check the URL of this post :
http://stackoverflow.com/questions/1467402/generate-a-unique-string-from-the-post-title-like-stackoverflow
The "real" unique part is the number -- here, 1467402
: it looks like the identifier of the question in the database ; probably some kind of auto-increment / sequence, which is ensured to be unique by the database server.
Actually, you can try to check by yourself if the "title" part matters : go to this URL :
http://stackoverflow.com/questions/1467402/glop
Even though the "title" part is obviously not here, that URL this gets you to this post ;-)
The "title" appearing in the URL is here for two reasons :
- more user friendly URLs, of course
- more search-engines / better for referencement URLs, too
To generate this, a couple of thing to do :
- replace non-ascii characters ; for instance, 'é' will most likely be translated to 'e'
- replace the other characters you can't replace nicely by '-' as a word-separator.
They just replace spaces with a dash, but then you will want to make certain that doesn't already exist.
If it does exist, just add a number to the end.
You notice that they have a number before the unique string, which will reduce the chance of a collision.
You could generate that based on the julian date (number day in the year) and a year, for example.
Phil Haack posted on this subject in this entry.
You could do something similar but more generic (add an incrementing number on the end).
If the post titles are unique just use those. Or you could create a message digest signature.
see: http://www.obviex.com/samples/hash.aspx for the long answer.