Reputation: 3
this is my first time asking for help here at StackOverflow.
I've been trying to work on a project which allows random string generation. Although it is working, I'm trying to find out how to add special character(s) after a certain amount of characters that have been generated.
This is my code:
Public Class Form1
Dim pool As String = ""
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
pool = ""
If CheckBox1.Checked = True Then
pool = pool & "0123456789"
End If
If CheckBox2.Checked = True Then
pool = pool & "abcdefghijklmnopqrstuvwxyz"
End If
If CheckBox3.Checked = True Then
pool = pool & "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
End If
Dim count = 1
Result.Text = ""
Dim cc As New Random
Dim strpos = ""
While count <= Length.Text
strpos = cc.Next(0, pool.Length)
Result.Text = Result.Text & pool(strpos)
count = count + 1
End While
End Sub
End Class
Now, I can generate the string, but I'm looking to find out how to add hyphens. For example I get "XikclCwXrPBd8RL35oaoN5LNW" when the string is generated at twenty-five characters. What I can't figure out, is how to add hyphens every fifth character, which it would look like this, "Xikcl-CwXrP-Bd8RL-35oao-N5LNW."
If I were to add code which generates hyphens every fifth (or any custom amount) character, would I have to redo my code again, or is the solution to my problem simple?
Thanks, and I hope this question isn't too much of a hassle.
Here is also a screenshot of my project. http://puu.sh/aEgus/0309527a1e.png I don't have "at least 10 reputation to post images."
Upvotes: 0
Views: 475
Reputation: 1652
This should do the trick
While count <= Length.Text
strpos = cc.Next(0, pool.Length)
Result.Text = Result.Text & pool(strpos)
If count MOD 5 = 0 And count < Length.Text Then
Result.Text = Result.Text & "-"
End If
count = count + 1
End While
I'm not sure if the VB syntax is good as I never coded VB before but I'm sure you'll be able to figure it out. It also takes care of not adding a hyphen at the end.
Upvotes: 1
Reputation: 170
If you are just looking to add it every 5th character, you could simply throw an if statement inside of your while loop checking to see if count%5 = 0. If so, add the character, add to the counter, and move on.
Example:
While count <= Length.Text
strpos = cc.Next(0, pool.Length)
If count MOD 5 = 0 Then
Result.Text = Result.Text & "-"
End If
Result.Text = Result.Text & pool(strpos)
count = count + 1
End While
Upvotes: 2
Reputation: 306
I cannot test it right now, but you can add "-" string once you have generated the random string. In this case you don't have to change your existing code. It's up to you decide which solutions suits better your needs.
Dim pos As Integer = 3
While pos < finalString.length
finalString = finalString.insert(pos, "-")
pos = pos + 5
End While
Basically, this solution use the insert
method of the string class in VB.NET to add the "-" string every four characters.
Again, you have to test it yourself :)
Upvotes: 0