|
-
May 3rd, 2007, 11:01 AM
#1
Thread Starter
New Member
VB String help
Ey up!
Currently underway with a college project - i've got to create a game of hangman. I've done every part except this last bit. I don't want to know how to do the game, or anything like that.. all i would like to know, is how i could replace a character within a string, at a position, with another.
For example, if i had "_ _ _ _" (without spaces) how could i replace ONLY the second letter with say an "E"...or a letter stored in a variable ?
Cheers!
-
May 3rd, 2007, 11:27 AM
#2
Re: VB String help
This works:
Code:
Dim Blanks As String
Private Sub Form_Load()
Blanks = String(5, "_")
Blanks = Left(Blanks, 1) & "E" & Mid(Blanks, 3)
MsgBox Blanks ' Returns _E___
End Sub
Last edited by Code Doc; May 3rd, 2007 at 11:30 AM.
Reason: Changed + to &
Doctor Ed
-
May 3rd, 2007, 11:34 AM
#3
Addicted Member
Re: VB String help
or:
Dim s as string
s = "-----"
Mid$(s,2,1) = "E"
-
May 3rd, 2007, 11:43 AM
#4
Addicted Member
Re: VB String help
I'd do something like this.. (though the one above works as well)
vb Code:
Dim s As String
Dim i As Integer
Dim hangString As String
s = "______" 'That is _ _ _ _ _ _
'Now, just to replace a certain char with another in a string, lets say, the 2 position
For i = 1 To Len(s)
If i = 2 Then
hangString = hangString & "E"
Else
hangString = hangString & "_"
End If
Next i
That would return _E____
You obviously wouldn't hard code the position (If i = 2) into the code. You'd have the position as a variable.
- If you found my post to be helpful, please rate me.

-
May 3rd, 2007, 12:20 PM
#5
Re: VB String help
MWagner said, "You obviously wouldn't hard code the position (If i = 2) into the code. You'd have the position as a variable."
--------------------
Agreed. One possibility is to use two list boxes. One list box contains the letters A-Z, The other contains the positions based on the length of the word. A text box shows the mystery word as it is being built.
Letters are added using a command button and the textbox is updated with the letter and the rest of the blanks. As positions are used up, they are removed from the second list box. As letters are used up, they are removed from the first list box. When the positions list box is empty, the word is spelled completely in the textbox. Game over.
Now all we have to do is draw the hanged man sequentially.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|