Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

generate a unique string from the post title like stackoverflow

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.

like image 450
Shyju Avatar asked Apr 26 '26 11:04

Shyju


2 Answers

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.
like image 111
Pascal MARTIN Avatar answered Apr 29 '26 01:04

Pascal MARTIN


This is a very broad question.

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

like image 24
Stan R. Avatar answered Apr 29 '26 01:04

Stan R.