【问题标题】:Visual Basic Replace is not workingVisual Basic 替换不起作用
【发布时间】:2016-10-11 01:51:48
【问题描述】:

我正在编写一个简单的刽子手程序,我想替换我的变量中的一些内容,该变量存储已找到的单词的字母。

代码如下:

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

wordLettersFound、counter 和 letter 是我使用的 3 个变量。

这个脚本前面的变量都是下划线,但是没有变化!谁能帮我解决这个问题?

附: 我不知道我使用的是什么版本的 VB,Visual Studio Community 2015 只是说“Visual Basic”。

【问题讨论】:

  • 您正在 VS 2015 中开发应用程序,但您使用的是旧版的 Replace()Mid() 函数,这些函数在 VS 2008 之前的版本中受支持?
  • @AndrewMorton :我很清楚这一点,但我认为人们不应该习惯这些旧方法,因为它们可能有一天会被删除。 -- 我认为所有这些旧方法都应该放在可兼容性命名空间中,并重写文档教程以包含新方法。
  • @VisualVincent 使用 VB.NET 方法而不是框架方法是有好处的,但是在这个问题上我们不要去那里。

标签: vb.net


【解决方案1】:

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

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

【讨论】:

  • 谢谢,我的代码现在更改了字母,但是更改后的字母位于最后一个更改的字母之后,而不是正确的位置,例如ship 是 sp__ 而不是 s__p。这是代码:
  • If Mid(wordLettersFound, counter, 1) = "_" Then wordLettersFound = Replace(wordLettersFound, Mid(wordLettersFound, counter, 1), letter, 1, 1, 0) End If
【解决方案2】:

另一种替换方式,

    Dim theLetters As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzAAA"

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

【讨论】:

  • 结果会是什么?
  • 啊,是的,但我想替换特定部分,而不是所有字符串。
  • 部分是什么意思?一组字符?
【解决方案3】:

还有另一种方法可以替换字符串中的字符。在您的情况下,使用 Replace 函数有点尴尬,因为在开始时,all 字符都是下划线 - 使用时替换会将所有字符替换为找到的字符。

相反,您可以将字符串剪切到所需替换左侧的部分,添加替换字符,然后添加字符串的其余部分。该行是此代码中注释“将 foundWord 切起来并将字符放在正确的位置”之后的行:

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

注意不要将所有这些代码直接用于家庭作业,因为您的老师找到它。这也适用于其他做家庭作业的人——你知道自己是谁;)

【讨论】:

  • 哈哈,这实际上不是家庭作业。虽然在我做这个一年后我们确实得到了一块,但到那时我已经学会了更容易使用的数组;)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-15
  • 2016-06-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多