有没有产生非重复的字符串的简单方法? [英] Is there a simple way to generate a un-duplicate string?

查看:121
本文介绍了有没有产生非重复的字符串的简单方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我可以使用GUID生成一个唯一的字符串,但它太长时间。
现在我只需要生成一个网站内的非重复的字符串,我该怎么办?谢谢!

I know I can use GUID to generate a unique string, but it's too long. Now I only need generate un-duplicate string within an website, how can I do? Thanks!

例如:在网站 http://mathurl.com/ ,您可以生成一个永久的URL,这样的作为 http://mathurl.com/75ujy7b 并75ujy7b很短,非重复的。

For example: In the website http://mathurl.com/, you can generate a permanent URL, such as http://mathurl.com/75ujy7b and 75ujy7b is very short and un-duplicate.

推荐答案

所有你可以用您确保使用互斥/锁返回唯一incrimental数字计数器开始,你的地方节省您的网站在过去数第一也许在web.config中,也许在数据库中,或在一个文件中。

First of all you can start with a counter that you make sure using Mutex/lock that return unique incrimental numbers, and you save the last number somewhere on your site, maybe in web.config, maybe in database, or in a file.

然后你这个独特的数字转换为不同的基数,如以一个base-64。下面是一些code,以及你如何能做到这模式的详细信息。

Then you convert this unique number to a different base number, eg to a base-64. Here is some code, and mode details on how you can do that

http://stackoverflow.com/a/5901201/159270

,你可以得到像

value: 0 encoded: A
value: 1 encoded: B
value: 9999999999 encoded: SrYsNt
value: 4294965286 encoded: ZNGEvT
value: 2292964213 encoded: rHd24J
value: 1000000000 encoded: TrNVzD

现在,如果你争我夺的字符映射你也可以做一个不那么容易找到号码。

Now, if you scramble the map on the characters you can also make a not so easy to find number.

这篇关于有没有产生非重复的字符串的简单方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆