Results 1 to 5 of 5

Thread: VB String help

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2007
    Posts
    1

    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!

  2. #2
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    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

  3. #3
    Addicted Member sigid's Avatar
    Join Date
    May 2006
    Location
    Massachusetts, USA
    Posts
    182

    Re: VB String help

    or:
    Dim s as string

    s = "-----"

    Mid$(s,2,1) = "E"

  4. #4
    Addicted Member
    Join Date
    Jun 2006
    Posts
    172

    Re: VB String help

    I'd do something like this.. (though the one above works as well)

    vb Code:
    1. Dim s As String
    2. Dim i As Integer
    3. Dim hangString As String
    4.  
    5. s = "______"        'That is _ _ _ _ _ _
    6.  
    7. 'Now, just to replace a certain char with another in a string, lets say, the 2 position
    8.  
    9. For i = 1 To Len(s)
    10.     If i = 2 Then
    11.         hangString = hangString & "E"
    12.     Else
    13.         hangString = hangString & "_"
    14.     End If
    15. 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.

  5. #5
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    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.
    Doctor Ed

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width