Littleoak 12
Littleoak 12

Reputation: 47

Visual Basic Replace is not working

I am writing a simple hangman program and I want to replace something in my variable which stores the letters of the word that have been found.

Here is the code:

    Replace(wordLettersFound, Mid(wordLettersFound, counter, 1), letter)

wordLettersFound, counter and letter are 3 of the variables I am using.

The variable is all underscores before this script, but it does not change! Can anyone help me with this?

P.S. I do not know what version of VB I am using, visual studio community 2015 just says 'visual basic'.

Upvotes: 0

Views: 1069

Answers (3)

Andrew Morton
Andrew Morton

Reputation: 25013

There is another way to replace a character in a string. Using the Replace function is a bit awkward in your case because, at the start, all the characters are underscores - Replace as you're using it will replace all of them with the found character.

Instead, you can cut up the string to the piece to the left of the desired replacement, add in the replacement character, and add on the rest of the string. That line is the one after the comment "chop foundWord up and put the character in the right place" in this code:

Module Module1

    Sub Main()
        Dim wordToFind = "alphabet"
        ' make a string of dashes the same length as the word to find
        Dim foundWord = New String("-"c, wordToFind.Length)

        While foundWord <> wordToFind
            Console.Write("Enter your guess for a letter: ")

            Dim guess = Console.ReadLine()
            ' make sure the user has only entered one character
            If guess.Length = 1 Then
                ' see if the letter is in the string
                Dim pos = wordToFind.IndexOf(guess)
                While pos >= 0
                    ' chop foundWord up and put the character in the right place
                    foundWord = foundWord.Substring(0, pos) & guess & foundWord.Substring(pos + 1)
                    ' see if there are any more of the same letter
                    pos = wordToFind.IndexOf(guess, pos + 1)
                End While

                ' show the user the current progress
                Console.WriteLine(foundWord)
            Else
                Console.WriteLine("Please enter just one letter!")
            End If

        End While

        Console.WriteLine("You did it!")

        Console.WriteLine("Press enter to leave the program.")
        Console.ReadLine()

    End Sub

End Module

N.B. Do not use all that code directly for homework because your teacher will find this. And that goes to anyone else doing homework - you know who you are ;)

Upvotes: 0

dbasnett
dbasnett

Reputation: 11773

Another way to do replace,

    Dim theLetters As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzAAA"

    theLetters = theLetters.Replace("A"c, "@"c)

Upvotes: 0

PatrickSteele
PatrickSteele

Reputation: 14677

Replace doesn't modify the string but returns a new string with the replacement so you should assign it to the variable:

wordLettersFound = Replace(wordLettersFound, Mid(wordLettersFound, counter, 1), letter)

Upvotes: 3

Related Questions