2013-05-03 106 views
1

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 

回答

4

如果要替換破折號字符串的所有字符,爲什麼不只是使僅由破折號一個新的字符串,這是同樣的長度起始字符串?這不是一回事嗎?

WordDisplayTextBox.Text = New String("_", WordString.Length) 
+0

我內心很努力找出如何在vb.net中做到這一點。如果我沒有弄錯,在舊的VB中,有一個特定的功能來做到這一點,我正在尋找它在Microsoft.VisualBasic.Strings命名空間... :) – ajakblackgoat 2013-05-03 15:07:10

+0

如果它在那裏它可能已被替換正則表達式。你也許可以做一些像Regex('a-z','A-Z',0-9)或者其他類似的東西(不太瞭解Regex),但對於初學者來說,這可能是一種矯枉過正的方式。 – AFischbein 2013-05-03 15:15:08

+0

發現VB6函數做同樣的,更容易... ['字符串(長度,字符)'](http://en.wikibooks.org/wiki/Visual_Basic/VB6_Command_Reference#String) – ajakblackgoat 2013-05-03 16:32:18

2

循環遍歷WordString的長度和每次寫「__」的WordDisplayTextBox

For i As Integer = 1 To Len(txtWordString.Text) 

     WordDisplayTextBox.Text += "__" + " " 'Space separates the dashes for clarity 
Next 
相關問題