Click here to Skip to main content
15,888,401 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I generate a random string and I want it to be same length every time but it can vary from 24 to 29 characters. If I want it to be 30 how can I pad the end of the string with random numbers so it will always be 30 characters long. So if its 24 characters it would add 6 extra characters or if its 25 it would add 5 extra random characters

What I have tried:

Tried making it longer than 30 and removing characters but couldn't get it to work

DateCode is just the date formatted in a certain way.
There is a function to calculate the CRC32 of a string
There is a list of unique serials in a list box

The CRC32 generated can vary in length which I only spotted when testing 5000 serials.

VB
For Each item As String In lstserials.Items
           
          file.WriteLine(item & "," & "PB" & LocationCode & txtReference.Text & DateCode & Crc32CalcString(System.Guid.NewGuid.ToString() & DateAndTime.Now.ToString & item))
            

        Next
Posted
Updated 29-Jan-20 5:04am
v2
Comments
Richard Deeming 29-Jan-20 10:34am    
If you want us to help you fix your code, then you need to show us the relevant parts of your code.

Click the green "Improve question" link and update your question to show the code you currently have, and explain what you've tried and where you're stuck.
Richard MacCutchan 29-Jan-20 11:06am    
You have update your post but the code you have added has nothing to do with the original question.

VB
Public Function PadString (length As Integer, original As String) As String
   Dim len As Integer = original.Length
   If len > length
      original = original.SubString(length)
   Else if len < length
      Dim rand As New Random
      While original.Length < length
         original += rand.Next(1, 10).ToString()
      End While
   End If
   Return original
End Function

There may be some prettier/quicker/smarter ways, this is just a quick coding from someone who doesn't do VB so much anymore.
 
Share this answer
 
v2
VB
Dim line As String
Dim lco As Integer
line = "the quick brown fox jumps"
Console.WriteLine("line: {0}, count: {1}", line, line.Count)
lco = line.Count
While lco < 30
    line += "*"
    lco += 1
End While
Console.WriteLine("line: {0}", line)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900