How to get random string with spaces and mixed case?

前端 未结 3 2016
隐瞒了意图╮
隐瞒了意图╮ 2021-01-18 13:48

I am in need of generating a random string with spaces and mixedCase.

This is all I got so far:

    /// 
    /// The Typing monkey gen         


        
3条回答
  •  花落未央
    2021-01-18 14:12

    You could start with an array of all the characters you'll allow

    private static readonly char[] ALLOWED = new [] { 'a', 'b', 'c' ... '9' };
    

    And then:

    {
        ...
        for (int i = 0; i < size; i++)
        {
            ch = ALLOWED[random.NextInt(0, ALLOWED.Length)];
            builder.Append(ch);
        }
    
        ...
    
        return builder.ToString();
    }
    
    return builder.ToString();
    

    I paraphrase, of course. I'm not certain about the syntax on random.NextInt(), but intelisense aught to help.

提交回复
热议问题