Generate random strings in vb.net

2019-04-28 08:10发布

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 random
7条回答
我欲成王,谁敢阻挡
2楼-- · 2019-04-28 08:38

Just be simple. for vb just do:

Public Function RandomString(size As Integer, Optional validchars As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz") As String
    If size < 1 Or validchars.Length = 0 Then Return ""
    RandomString = ""
    Randomize()
    For i = 1 To size
        RandomString &= Mid(validchars, Int(Rnd() * validchars.Length) + 1, 1)
    Next
End Function

This function allows for a base subset of chars to use or user can choose anything. For example you could send ABCDEF0123456789 and get random Hex. or "01" for binary.

查看更多
登录 后发表回答