独特的随机字符串代 [英] Unique random string generation

查看:82
本文介绍了独特的随机字符串代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过生成MSDN库生成像那些随机唯一的字符串:

I'd like to generate random unique strings like the ones being generated by MSDN library:

<一个href=\"http://msdn.microsoft.com/en-us/library/t9zk6eay.aspx\">http://msdn.microsoft.com/en-us/library/t9zk6eay.aspx,例如。像t9zk6eay的字符串应该产生。

http://msdn.microsoft.com/en-us/library/t9zk6eay.aspx, for example. A string like 't9zk6eay' should be generated.

推荐答案

使用的Guid将是pretty好办法,但要得到的东西看起来像你的榜样,你可能想将其转换为一个Base64字符串:

Using Guid would be a pretty good way, but to get something looking like your example, you probably want to convert it to a Base64 string:

    Guid g = Guid.NewGuid();
    string GuidString = Convert.ToBase64String(g.ToByteArray());
    GuidString = GuidString.Replace("=","");
    GuidString = GuidString.Replace("+","");

我摆脱=和+来获得一点点接近你的榜样,否则你得到==在你的字符串中间的结束和一个+。下面是一个例子输出字符串:

I get rid of "=" and "+" to get a little closer to your example, otherwise you get "==" at the end of your string and a "+" in the middle. Here's an example output string:

OZVV5TpP4U6wJthaCORZEQ

"OZVV5TpP4U6wJthaCORZEQ"

这篇关于独特的随机字符串代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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