Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools

RandomString - Generate a random string using a mask
[ All Languages » VB »  String]

Total Hit ( 2306)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article

Rating


 



Click here to copy the following block
' generate a random string
'
' the mask can contain the following special chars
'  ? : any ASCII character (1-127)
'  # : a digit
'  A : an alphabetic char
'  N : an alphanumeric char
'  H : an hex char
' all other chars are taken literally

' Example: a random-generated phone number
'  phone = RandomString("(###)-####-####")


Function RandomString(ByVal mask As String) As String
  Dim i As Integer
  Dim acode As Integer
  Dim options As String
  Dim char As String
  
  ' initialize result with proper lenght
  RandomString = mask
  
  For i = 1 To Len(mask)
    ' get the character
    char = Mid$(mask, i, 1)
    Select Case char
      Case "?"
        char = Chr$(1 + Rnd * 127)
        options = ""
      Case "#"
        options = "0123456789"
      Case "A"
        options = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
      Case "N"
        options = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0" _
          & "123456789"
      Case "H"
        options = "0123456789ABCDEF"
      Case Else
        ' don't modify the character
        options = ""
    End Select
  
    ' select a random char in the option string
    If Len(options) Then
      ' select a random char
      ' note that we add an extra char, in case RND returns 1
      char = Mid$(options & Right$(options, 1), 1 + Int(Rnd * Len(options) _
        ), 1)
    End If
    
    ' insert the character in result string
    Mid(RandomString, i, 1) = char
  Next

End Function


Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.