根据2个符号(字母和数字)并使用-

时间:2019-06-03 07:52:47

标签: vb.net

这是一个数字 这是数字还是字母?

分隔随机字符串,例如??#?#-??? ##-#?#???-#???#-##

我需要一些生成字符串的代码,如上所述。不必太复杂。

预期结果示例:2F421-QD421-2W3FY0-3F4L1-37

我已经尝试过使用PHP和此示例,但无法实现我一直在寻找的Generating a random numbers and letters

我正在寻找一个vb.net项目来处理世代,以便可以将序列号手动提交到数据库中。

3 个答案:

答案 0 :(得分:1)

我很喜欢这种方法:

Dim characters = "0123456789ABCDEFGHIJKLOMNOPQRSTUVWXYZ"
Dim template = "??#?#-???##-#?#???-#???#-##"

Dim rnd = New Random()

Dim query =
    From t In template
    Select If(t = "-", "-", characters(rnd.Next(If(t = "?", characters.Length, 10))))

Dim result = String.Join("", query)

Console.WriteLine(result)

它给我这样的输出:

  

RC2C9-DHB47-1Q07RL-8BIF7-57

答案 1 :(得分:0)

为字母GRL(生成随机字母)创建2个功能1,为数字GRN(生成随机数字)创建1个函数,如下所示: 我所说的结果是:W96-GKlF6

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Console.WriteLine(GRL(1) + GRN(2) + "-" + GRL(4) + GRN(1))
End Sub

Public Function GRL(ByRef iLength As Integer) As String
    Static rdm As New Random()
    Dim allowChrs() As Char = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLOMNOPQRSTUVWXYZ".ToCharArray()
    Dim sResult As String = String.Empty

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

    Return sResult
End Function

Public Function GRN(ByRef iLength As Integer) As String
    Static rdm As New Random()
    Dim allowChrs() As Char = "0123456789".ToCharArray()
    Dim sResult As String = String.Empty

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

    Return sResult
End Function

答案 2 :(得分:0)

用作ASCII码的简单,随机数,然后检查位置以界定其范围是否只是数字或可以是数字或字母的字符。

当位置可以是数字或字母时,请分析随机数并将其拆分。如果数字小于11,则表示是一个数字,然后加47,然后将结果用作ASCII码(从1到36随机创建数字),例如,如果随机数为1,我们说47 + 1 = 48,则48为ASCII代码0。

如果数字为11或更大,我们加54,例如,如果随机数为11,则我们有11 + 54 =65。65是字母A的ASCII码。

    Dim Key As String = ""
    Dim N As Integer
    Randomize()
    For t = 1 To 23
        If t = 3 Or t = 5 Or t = 9 Or t = 10 Or t = 11 Or t = 13 Or t = 17 Or t >= 21 Then
            N = 10
        Else
            N = 36
        End If
        Dim value As Integer = CInt(Int(N * Rnd() + 1))
        If value < 11 Then
            Key = Key & Chr(value + 47)
        Else
            Key = Key & Chr(value + 54)
        End If
        If t = 5 Or t = 10 Or t = 16 Or t = 21 Then
            Key = Key & "-"
        End If
    Next