VB 2010 - 初学者在这里, 我为一个任务创建一个hang子手游戏,而且我无法用短划线替换字符串的文本。我不确定是否需要将字符串转换为charArray(),或者是否可以使用string.Replace函数。我知道我需要循环它不确定如何..VB 2010 - 用破折号代替字符串的所有字母
非常困惑,我需要一些帮助。由于我正在学习,请尽量保持简单,并请理性。
我的沙盒到目前为止代码:
Imports System.IO
Public Class Form1
Private Const TEST = "test.txt"
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim WordString As String = Nothing
Dim NewWordInteger As Integer
Dim RandomWord As New Random(System.DateTime.Now.Millisecond) 'Load a new word at the time it was initiated
'Load words from test file into the array
Dim WordArray() As String = File.ReadAllLines(TEST) 'Reads words from list and declares each as a string
'Select Random word from the number of words in the dictionary.txt file
NewWordInteger = RandomWord.Next(0, 4)
'Display RandomWord in textbox as STRING..
WordString = WordArray(NewWordInteger) ' Assigns wordstring a word from the arrany & random by the NewWordInterger Substring..
WordDisplayTextBox.Text = WordString ' will display the word in the textbox
SolveLabel.Text = WordString ' will display the word in the Solve label
'Will shoe the array word and the word/string position in the TEST.file
ListBox1.Items.Add(WordString) ' will show the word
ListBox2.Items.Add(NewWordInteger) ' will show the string position in the TEST.file
'search string and replace letters with _ (Dashes)
Dim charArray() As Char = WordDisplayTextBox.Text.ToCharArray
For Each item As Char In WordDisplayTextBox.Text
WordDisplayTextBox.Text = WordString.Replace(item, "_")
Next
End Sub
End Class
我内心很努力找出如何在vb.net中做到这一点。如果我没有弄错,在旧的VB中,有一个特定的功能来做到这一点,我正在寻找它在Microsoft.VisualBasic.Strings命名空间... :) – ajakblackgoat 2013-05-03 15:07:10
如果它在那里它可能已被替换正则表达式。你也许可以做一些像Regex('a-z','A-Z',0-9)或者其他类似的东西(不太了解Regex),但对于初学者来说,这可能是一种矫枉过正的方式。 – AFischbein 2013-05-03 15:15:08
发现VB6函数做同样的,更容易... ['字符串(长度,字符)'](http://en.wikibooks.org/wiki/Visual_Basic/VB6_Command_Reference#String) – ajakblackgoat 2013-05-03 16:32:18