0

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."

3 Answers 3

2

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
Sign up to request clarification or add additional context in comments.

6 Comments

Ah, see, this is exactly what I figured. But the issue now is, I don't know how to exactly code that.
@NoNameBlank - for visual basic it's the MOD function
Just replace the "%" with " MOD " :)
I'm actually getting an error with this IF block. If count MOD 5 == 0 Then Result.Text = Result.Text & "-" End If I'm getting an error with "==" and Error List says "Expression Expected"
this may be a fail c# to VB conversion...try a single = sign
|
1

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.

13 Comments

The If statement requires a Then and I believe the MOD check needs to be done before adding a new character otherwise in this case you would have it added in the wrong place ie starting at character 6 instead of character 5.
count has an initial value of 1. My sample will insert a hyphen after inserting 5 letters exactly as in the question example: "Xikcl-CwXrP-Bd8RL-35oao-N5LNW". I added the Then (as I mentioned in the answer I'm not familiar with VB syntax). I don't think that a syntax error is worth a downvote by the way, a comment was more than enough.
@Zammalad I actually get an expression error with the "==" using your solution from above "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"
@Gabriel Apologies I missed the count initialization. Your method is indeed correct
@NoNameBlank Sorry yes it should be a single = sign not == (classic C++ coder mistake) This solution by Gabriel will work though, use that.
|
0

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 :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.