简体   繁体   English

Visual Basic替换无法正常工作

[英]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. 我正在编写一个简单的hangman程序,我想替换变量中存储了所找到单词的字母的内容。

Here is the code: 这是代码:

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

wordLettersFound, counter and letter are 3 of the variables I am using. wordLettersFound,counter和letter是我正在使用的3个变量。

The variable is all underscores before this script, but it does not change! 该脚本之前的变量都是下划线,但是不会改变! Can anyone help me with this? 谁能帮我这个?

PS I do not know what version of VB I am using, visual studio community 2015 just says 'visual basic'. PS我不知道我正在使用哪个版本的VB,Visual Studio Community 2015只是说“ Visual Basic”。

Replace不会修改字符串,但会返回带有替换的新字符串,因此您应将其分配给变量:

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

Another way to do replace, 另一种替换方法

    Dim theLetters As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzAAA"

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

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: 该行是此代码中的注释“将founddchoup并将字符放在正确的位置”之后的那一行:

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

NB 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 ;) 这也适用于其他家庭作业-您知道您是谁;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM