views:

206

answers:

5

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.

+16  A: 

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.
Pascal MARTIN
+1 - I never thought about that number being the database id.
James Black
I'll admit, I lol'd at "glop"... I may need more sleep.
Greg Hurlman
@James : using an auto-increment in database looks like the easier way to me (and it's what I generally use), so, just guessing, but why not ;;; @Greg : I need more sleep too, actually ^^
Pascal MARTIN
Take note: Even though it is optional, the unique string **is** directly associated with url. Check out the canonical element in the source of stackoverflow.com/questions/1467402 . Little things like that make search engines happier.
Brian
does glop mean anything or is it you just randomly selecting 4 characters? ;)
ming yeow
@ming it's not random : in France, some many years ago, there was a comic with a dog called Pifou that said "glop glop !" when he was happy *(or something was nice)*, and "pas glop ! pas glop !" *("not glop !")* when he wasn't *(or something wasn't ok)* -- a search for "pifou" on google images should get you some examples ;-) ;;; I often use "glop" where/when someone else could say "foo" or "bar", except those are not used in french.
Pascal MARTIN
+1  A: 

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.

James Black
A: 

Phil Haack posted on this subject in this entry.

You could do something similar but more generic (add an incrementing number on the end).

Will
+2  A: 

This is a very broad question.

Most of the time when I need to identify something unique, I use a Guid.

Stan R.
A: 

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.

Lance Rushing