-1

我在 vb.net 中有一个这样的随机函数,它返回这样的 6 位字母数字字符串。

Public Function rand() As String
        Dim rng As Random = New Random
        Dim sb As New StringBuilder

        ' Selection of pure numbers sequence or mixed one
        Dim pureNumbers = rng.Next(1, 11)
        If pureNumbers < 7 Then
            ' Generate a sequence of only digits
            Dim number As Integer = rng.Next(1, 1000000)
            Dim digits As String = number.ToString("000000")
            For i As Integer = 1 To 6
                Dim idx As Integer = rng.Next(0, digits.Length)
                sb.Append(digits.Substring(idx, 1))
            Next
        Else
            ' Generate a sequence of digits and letters 
            Dim s As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
            For i As Integer = 1 To 6
                Dim idx As Integer = rng.Next(0, 36)
                sb.Append(s.Substring(idx, 1))
            Next
        End If
        Return sb.ToString()
    End Function

问题是当它返回时0or Oor 1orI 等​​等{"1", "I", "O", "0", "5", "S", "7"}

我想用'A'替换​​这些字符,然后应该返回最终的随机字符串。

4

1 回答 1

0

如果我理解正确,你想要这样的东西(未经测试):

 dim exceptionLetters = _
     {"1", "I", "O", "0", "5", "S", "7"}

 Dim s As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
 For i As Integer = 1 To 6
     Dim idx As Integer = rng.Next(0, 36)
     Dim ltr = s.Substring(idx, 1)
     if (exceptionLetters.Contains(ltr))
         Return sb
     End If
     sb.Append(ltr);
 Next
于 2013-09-30T06:19:43.673 回答