HOWTO - 生成给定长度的字母数字字符串 [英] HOWTO - generate a alphanumeric string of a given length
问题描述
是否有函数返回一些随机ID,如字符串字母数字?
喜欢这样:
A35sDsd1dSGsH
谢谢
Crirus
试试这个 - OHM
公共类StringGenerator
公共共享函数RandomString(length as Integer)As String
Const zero As Integer = CInt(" 0" c)
Const 9 As Integer = CInt(9c)
Const capA As Integer = CInt(Ac)
Const capZ As Integer = CInt(" ; Z" c)
Dim guid As Byte()= Guid.NewGuid()。ToByteArray()
Dim seed As Integer = guid(0) + Machine.Shift.Left(guid(1),8)+
Machine.Shift.Left(guid(2),16)+ Machine.Shift.Left(guid(3),24)
Dim r As New Random(种子)
Dim s As String = Nothing
Dim i As Integer
For i = 0 to length - 1
Dim n As Integer
Do
n = r.Next(零,capZ)
循环而不是(n> = 0且n <= 9或(n> = capA)并且n< = capZ))
s + = CChar(n)
接下来我
返回s
结束函数''RandomString
结束类''StringGenerator
Crirus写道:是有一个函数返回一些随机ID,如字符串
字母数字?像这样:
A35sDsd1dSGsH
谢谢
Crirus
HI OHM,
不仅是随机的,而是在Ohandedcator中构建。
:-))
Cor
在文章<#5 ************** @ TK2MSFTNGP10.phx.gbl>中,Crirus写道:是否有一个返回一些随机ID的函数,比如字符串字母数字?
像这样:
A35sDsd1dSGsH
谢谢
Crirus
你想要多长时间?
Dim id As String = Guid.NewGuid()。ToString()。Replace(" - ",String .Empty)
Console.WriteLine(id)
这将产生一个32位字符串的十六进制数字(0-9和af),>
在统计上是独一无二的(基本上,它几乎是有价值的
它永远不会重复)。
-
Tom Shelton
MVP [Visual Basic]
Is there a function that return some random ID like string alphanumeric?
Like this:
A35sDsd1dSGsH
Thanks
Crirus
Try this - OHM
Public Class StringGenerator
Public Shared Function RandomString(length As Integer) As String
Const zero As Integer = CInt("0"c)
Const nine As Integer = CInt("9"c)
Const capA As Integer = CInt("A"c)
Const capZ As Integer = CInt("Z"c)
Dim guid As Byte() = Guid.NewGuid().ToByteArray()
Dim seed As Integer = guid(0) + Machine.Shift.Left(guid(1), 8) +
Machine.Shift.Left(guid(2), 16) + Machine.Shift.Left(guid(3), 24)
Dim r As New Random(seed)
Dim s As String = Nothing
Dim i As Integer
For i = 0 To length - 1
Dim n As Integer
Do
n = r.Next(zero, capZ)
Loop While Not(n >= zero And n <= nine Or(n >= capA And n <= capZ))
s += CChar(n)
Next i
Return s
End Function ''RandomString
End Class ''StringGenerator
Crirus wrote:Is there a function that return some random ID like string
alphanumeric? Like this:
A35sDsd1dSGsH
Thanks
Crirus
HI OHM,
Not only random but with build in Ohandedcator.
:-))
Cor
In article <#5**************@TK2MSFTNGP10.phx.gbl>, Crirus wrote:Is there a function that return some random ID like string alphanumeric?
Like this:
A35sDsd1dSGsH
Thanks
Crirus
How long do you want it?
Dim id As String = Guid.NewGuid().ToString().Replace("-", String.Empty)
Console.WriteLine(id)
That will produce a 32 character string of hex digits (0-9 and a-f),
that is statistically unique (basically, it is pretty much guarented
that it will never be duplicated).
--
Tom Shelton
MVP [Visual Basic]
这篇关于HOWTO - 生成给定长度的字母数字字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!