本文介绍了如何在vb.net中生成自己的自定义api密钥?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 大家好, 我有问题。我有一些生成API密钥的代码。前面需要一个字母,后面需要19个数字。问题是我的代码不会将第一个数字转换为字母。 这是我的代码(它很长而且很慢),如果你可以改进它,请随意这样做: 私有 功能 GenerateAPI()作为 字符串 Dim 字母 As String Dim number As String Randomize() number = Int(Rnd()* 26 )+ 1 letter = number.Replace( 1 , a) letter = number.Replace( 2 , b) letter = number.Replace( 3 , c) letter = number.Replace( 4 , d) letter = number.Replace( 5 , e) letter = number.Replace( 6 , f) letter = number.Replace( 7 , g) letter = number.Replace( 8 , h) letter = number.Replace( 9 , i) letter = number.Replace( 10 , j) letter = number.Replace( 11 , k) letter = number.Replace( 12 , l) letter = number.Replace( 13 , m) letter = number.Replace( 14 , n) letter = number.Replace( 15 , o) letter = number.Replace( 16 , p) letter = number.Replace( 17 , q) letter = number.Replace( 18 , r) letter = number.Replace( 19 , s ) letter = number.Replace( 20 , t) letter = number.Replace( 21 , u) letter = number.Replace( 22 , v) letter = number.Replace( 23 , w) letter = number.Replace( 24 , x) letter = number.Replace( 25 , y) letter = number.Replace( 26 , z) GenerateAPI = letter MsgBox(letter) 对于 i 作为 整数 = 0 至 19 Randomize() Dim x As Integer = Int(Rnd()* 9 )+ 1 GenerateAPI& = x 下一步 结束 功能 提前谢谢。解决方案 这是一个快速和令人讨厌的解决方案...我省略了字母I和O,因为它们很容易被混淆为1和0 ... 只是几条评论... 1.字符串是不可变的,字符串不可变有许多优点。它提供自动线程安全性,并使字符串以简单有效的方式表现为内在类型。这允许在运行时提高效率(例如允许有效的字符串实习以减少资源使用),并且具有巨大的安全优势,因为第三方API调用不可能更改字符串。 添加StringBuilder是为了解决不可变字符串的一个主要缺点 - 不可变类型的运行时构造会导致很多GC压力并且本质上很慢。通过创建一个显式的,可变的类来处理这个问题,解决了这个问题,而没有给字符串类增加不必要的复杂性。 2. ToCharArray()函数将允许你快速将字符串分解为可用于第一个字符的字符数组。 Dim r As 新 Random() Dim x = abcdefghjklmnpqrstuvwxy Dim xx = x.ToCharArray() Dim sb = 新 StringBuilder() sb.Append(xx(r。[下一步]( 0 ,xx.Length))) 对于 i As 整数 = 0 18 sb。追加(r。[下一步]( 0 , 10 )) 下一步 Console.WriteLine(sb.ToString()) Console.ReadKey() PS请不要那么苛刻......我们都在某个地方开始...... 是......你可以...... 那会稍微简化代码... Dim r As New Random() Dim x = abcdefghjklmnpqrstuvwxy Dim sb = New StringBuilder() sb.Append(x(r。[Next]( 0 ,x.Length)) )对于i as Integer = 0 至 18 sb.Append(r 。[Next]( 0 , 10 ))下一个 Console.WriteLine(sb.ToString()) Console.ReadKey() Hi guys,I have a problem. I have some code which generates an API key. It needs one letter at the front and 19 numbers after it. The problem is my code won't convert the first number into a letter.Here is my code (it is long and slow), if you can improve it, please feel free to do so:Private Function GenerateAPI() As String Dim letter As String Dim number As String Randomize() number = Int(Rnd() * 26) + 1 letter = number.Replace(1, "a") letter = number.Replace(2, "b") letter = number.Replace(3, "c") letter = number.Replace(4, "d") letter = number.Replace(5, "e") letter = number.Replace(6, "f") letter = number.Replace(7, "g") letter = number.Replace(8, "h") letter = number.Replace(9, "i") letter = number.Replace(10, "j") letter = number.Replace(11, "k") letter = number.Replace(12, "l") letter = number.Replace(13, "m") letter = number.Replace(14, "n") letter = number.Replace(15, "o") letter = number.Replace(16, "p") letter = number.Replace(17, "q") letter = number.Replace(18, "r") letter = number.Replace(19, "s") letter = number.Replace(20, "t") letter = number.Replace(21, "u") letter = number.Replace(22, "v") letter = number.Replace(23, "w") letter = number.Replace(24, "x") letter = number.Replace(25, "y") letter = number.Replace(26, "z") GenerateAPI = letter MsgBox(letter) For i As Integer = 0 To 19 Randomize() Dim x As Integer = Int(Rnd() * 9) + 1 GenerateAPI &= x Next End FunctionThanks in advance. 解决方案 Here is a quick and nasty solution... I have omitted the letters I and O since they can be easily confused for 1 and 0...Just a couple of comments...1. Strings are immutable, strings immutable has many advantages. It provides automatic thread safety, and makes strings behave like an intrinsic type in a simple, effective manner. This allows for extra efficiencies at runtime (such as allowing effective string interning to reduce resource usage), and has huge security advantages, since it's impossible for an third party API call to change your strings.StringBuilder was added in order to address the one major disadvantage of immutable strings - runtime construction of immutable types causes a lot of GC pressure and is inherently slow. By making an explicit, mutable class to handle this, this issue is addressed without adding unneeded complication to the string class.2. The ToCharArray() function will allow you to quickly break a string into a character array that you can use for your first character.Dim r As New Random()Dim x = "abcdefghjklmnpqrstuvwxy"Dim xx = x.ToCharArray()Dim sb = New StringBuilder()sb.Append(xx(r.[Next](0, xx.Length)))For i As Integer = 0 To 18 sb.Append(r.[Next](0, 10))NextConsole.WriteLine(sb.ToString())Console.ReadKey()PS Please don't be so harsh... We all started somewhere...Yes... you can...That would simplify the code somewhat...Dim r As New Random()Dim x = "abcdefghjklmnpqrstuvwxy"Dim sb = New StringBuilder()sb.Append(x(r.[Next](0, x.Length)))For i As Integer = 0 To 18sb.Append(r.[Next](0, 10))NextConsole.WriteLine(sb.ToString())Console.ReadKey() 这篇关于如何在vb.net中生成自己的自定义api密钥?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 07-31 12:03