本文介绍了随机生成字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 大家好, 我想生成固定长度的随机字符串。代码如下。Hi All,I would like to generate Random string of fixed length.The code is below.Private Shared Function RandomString(Size As Integer) As String Dim input As String = "abcdefghijklmnopqrstuvwxyz0123456789" Dim chars = Enumerable.Range(0, Size).[Select](Function(x) input(Random.[Next](0, input.Length))) Return New String(chars.ToArray()) End Function 但我收到错误But I receive an error of Quote:重载决策失败,因为没有可访问的'Select'可以使用这些参数调用: 扩展方法'公共函数选择(Of TResult)(selector As System.Func(Of Integer) ,Integer,TResult))作为System.Collections.Generic.IEnumerable(Of TResult)'在'System.Linq.Enumerable'中定义:嵌套函数没有与委托'System.Func兼容的签名(Of Integer,Integer) ,TResult)'。 扩展方法'Public Function Select(Of TResult)(selector As System.Func(Of Integer,Integer,TResult))As System.Collections.Generic.IEnumerable(Of TResult) '定义在'System.Linq.Enumerable'中:无法从这些参数推断出类型参数的数据类型。明确指定数据类型可能会纠正此错误。Overload resolution failed because no accessible 'Select' can be called with these arguments:Extension method 'Public Function Select(Of TResult)(selector As System.Func(Of Integer, Integer, TResult)) As System.Collections.Generic.IEnumerable(Of TResult)' defined in 'System.Linq.Enumerable': Nested function does not have a signature that is compatible with delegate 'System.Func(Of Integer, Integer, TResult)'.Extension method 'Public Function Select(Of TResult)(selector As System.Func(Of Integer, Integer, TResult)) As System.Collections.Generic.IEnumerable(Of TResult)' defined in 'System.Linq.Enumerable': Data type(s) of the type parameter(s) cannot be inferred from these arguments. Specifying the data type(s) explicitly might correct this error. 问候 SreejithRegardsSreejith推荐答案Private Shared chars As String = "abcdefghijklmnopqrstuvwxyz0123456789"Private random As New Random()Private Function RandomString(size As Integer) As String Return New String(Enumerable.Repeat(chars, size).[Select](Function(s) s(random.[Next](s.Length))).ToArray())End FunctionPrivate Function RandomString(size As Integer) As StringDim input As String = "abcdefghijklmnopqrstuvwxyz0123456789"Dim r As New RandomDim qry = Enumerable.Range(0, Size).[Select](Function(x) input(r.Next(0, input.Length)))Return New String(qry.ToArray())End Function 使用和结果:Usage and result:Dim a As String = RandomString(55)'result: y0lchxdvh8yaz73tbu1q0h6uptpkm19ldso8ozm64pnk7bvum86uz9ePrivate Shared chars As String = "abcdefghijklmnopqrstuvwxyz0123456789"Private random As New Random()Private Function RandomString(size As Integer) As String Return New String(Enumerable.Repeat(chars, size). [Select](Function(s) s( random. [Next](s.Length))) .ToArray())End Function 将这些位组合成一行!Assemble the bits into a single line! 这篇关于随机生成字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-23 09:25