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

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

问题描述

我想生成像 MSDN 库生成的那样的随机唯一字符串.(错误对象),例如.应该生成像 't9zk6eay' 这样的字符串.

I'd like to generate random unique strings like the ones being generated by MSDN library.(Error Object), for example. A string like 't9zk6eay' should be generated.

推荐答案

使用 Guid 将是一个不错的方法,但要获得类似于您的示例的内容,您可能需要将其转换为 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天全站免登陆