在vb.net中生成随机字符串 [英] Generate random strings in vb.net

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

问题描述

我需要在vb.net中生成随机字符串,该字符串必须由(随机选择的)字母A-Z(必须大写)组成,并且散布着随机数.它还必须能够生成具有设定长度的它们.

I need to generate random strings in vb.net, which must consist of (randomly chosen) letters A-Z (must be capitalized) and with random numbers interspersed. It needs to be able to generate them with a set length as well.

感谢您的帮助,这让我发疯了!

Thanks for the help, this is driving me crazy!

推荐答案

如果您可以将其转换为VB.NET(这很简单),我会说您很好(如果不能,请使用或任何其他有价值的工具):

If you can convert this to VB.NET (which is trivial) I'd say you're good to go (if you can't, use this or any other tool for what's worth):

/// <summary>
/// The Typing monkey generates random strings - can't be static 'cause it's a monkey.
/// </summary>
/// <remarks>
/// If you try hard enough it will eventually type some Shakespeare.
/// </remarks>
class TypingMonkey
{
   private const string legalCharacters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";

   static Random random = new Random();

   /// <summary>
   /// The Typing Monkey Generates a random string with the given length.
   /// </summary>
   /// <param name="size">Size of the string</param>
   /// <returns>Random string</returns>
   public string TypeAway(int size)
   {
       StringBuilder builder = new StringBuilder();
       char ch;

       for (int i = 0; i < size; i++)
       {
           ch = legalCharacters[random.Next(0, legalCharacters.Length)];
           builder.Append(ch);
       }

       return builder.ToString();
    }
}

然后您要做的就是:

TypingMonkey myMonkey = new TypingMonkey();
string randomStr = myMonkey.TypeAway(size);

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

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