按顺序生成“aa”,“Aa”,“aA”,“AA”等

时间:2013-05-13 04:47:50

标签: vb.net

如何按顺序生成2个小写/大写字母/数字的组合?

Sub Main()

    Dim Looper As Boolean = True

    While Looper = True
        'String = "aa", "Aa", "aA", "AA"
        'WebClient.DownloadString("link.com/" & String")
    End While

End Sub

像这样,但为String

生成组合

2 个答案:

答案 0 :(得分:1)

您可以循环遍历字符串,只需根据需要操作大写和小写:

Sub Main()
    Dim results as List(of String) = new List(of String)()

    For Each c as Char in "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray()
        results.Add(c.ToString().ToLower() & c.ToString().ToLower())
        results.Add(c.ToString() & c.ToString().ToLower())
        results.Add(c.ToString().ToLower() & c.ToString())
        results.Add(c.ToString() & c.ToString())
    Next
End Sub

答案 1 :(得分:0)

这将生成两个随机字符的组合,包括数字和大写/小写字母:

Public Function GetRandomString(ByVal iLength As Integer) As String
    Dim sResult As String = ""
    Dim rdm As New Random()

    For i As Integer = 1 To iLength
        sResult &= ChrW(rdm.Next(32, 126))
    Next

    Return sResult
End Function

或者您可以使用定义有效字符的公共随机字符串:

Public Function GenerateRandomString(ByRef iLength As Integer) As String
    Dim rdm As New Random()
    Dim allowChrs() As Char = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLOMNOPQRSTUVWXYZ0123456789".ToCharArray()
    Dim sResult As String = ""

    For i As Integer = 0 To iLength - 1
        sResult += allowChrs(rdm.Next(0, allowChrs.Length))
    Next

    Return sResult
End Function