Question:
change text in a string?
andrewed1
2013-05-03 21:24:14 UTC
in VB 2010 i need to change the letters of a string
ie "word" to display as: _ _ _ _ This is for an assignment of hangman. so i will also need to keeping the actual letters so they can be re-use and display if correctly guessed. as an example: _ o _ d

Questions
1) do i need to loop though the string and use the .replace fuction?
2). can i change each letter to a string? how?
3) do i need to convert the string to a charArrary()? how?

another method of completed this..?



My sandbox code so far:

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(NewWordInill display the word in the Solve label

'''''''''''Will show the array word and the word/string position in the TEST.file
ListBox1.Items.Add(WordString) ''''''''' wteger) '''''''' 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 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
Four answers:
texasmaverick
2013-05-04 14:51:26 UTC
This the code for preparing a place to guess the letters and to determine if they are correct or incorrect. It will also indicate when the word guessed is correct.



I am emailing you the details and instructions



Public Class Form1



Public Str1, Str2 As String

Public Flag1 As Integer



Private Sub Button1_Click_1(sender As System.Object, e As System.EventArgs) Handles Button1.Click

Str1 = TextBox11.Text

Label1.Text = Str1

Dim Num As Integer

Num = Len(Str1)

If Len(Str1) = 4 Then

TextBox5.Visible = False

TextBox6.Visible = False

TextBox7.Visible = False

TextBox8.Visible = False

TextBox9.Visible = False

TextBox10.Visible = False

ElseIf Len(Str1) = 5 Then

TextBox6.Visible = False

TextBox7.Visible = False

TextBox8.Visible = False

TextBox9.Visible = False

TextBox10.Visible = False

ElseIf Len(Str1) = 6 Then

TextBox7.Visible = False

TextBox8.Visible = False

TextBox9.Visible = False

TextBox10.Visible = False

ElseIf Len(Str1) = 7 Then

TextBox8.Visible = False

TextBox9.Visible = False

TextBox10.Visible = False

ElseIf Len(Str1) = 8 Then

TextBox9.Visible = False

TextBox10.Visible = False

ElseIf Len(Str1) = 9 Then

TextBox10.Visible = False

End If

End Sub





Private Sub TextBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox1.TextChanged



If Flag1 = 1 Then

If Trim(TextBox1.Text) = Mid(Str1, 1, 1) Then

MsgBox("You are correct")

Else

MsgBox("You are incorrect")

End If

Str2 = CheckString()

If Str2 = Str1 Then

MsgBox("you won")

'place the code you want here

End If

End If

Timer1.Enabled = True

End Sub



Function CheckString()

Str2 = Trim(TextBox1.Text) & Trim(TextBox2.Text) & Trim(TextBox3.Text) _

& Trim(TextBox4.Text) & Trim(TextBox5.Text) & Trim(TextBox6.Text) & Trim(TextBox7.Text) _

& Trim(TextBox8.Text) & Trim(TextBox9.Text) & Trim(TextBox10.Text)

Return Str2

End Function



Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load



Button1.Focus()



End Sub





Private Sub TextBox2_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox2.TextChanged

If Flag1 = 1 Then

If Trim(TextBox2.Text) = Mid(Str1, 2, 1) Then

MsgBox("You are correct")

Else

MsgBox("You are incorrect")

End If

Str2 = CheckString()

If Str2 = Str1 Then

MsgBox("you won")

'place the code you want here

End If

End If

Timer1.Enabled = True

End Sub



Private Sub TextBox3_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox3.TextChanged

If Flag1 = 1 Then

If Trim(TextBox3.Text) = Mid(Str1, 3, 1) Then

MsgBox("You are correct")

Else

MsgBox("You are incorrect")

End If

Str2 = CheckString()

If Str2 = Str1 Then

MsgBox("you won")

'place the code you want here

End If

End If

Timer1.Enabled = True

End Sub



Private Sub TextBox4_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox4.TextChanged

If Flag1 = 1 Then



If Trim(TextBox4.Text) = Mid(Str1, 4, 1) Then

MsgBox("You are correct")

Else

MsgBox("You are incorrect")

End If

Str2 = CheckString()

If Str2 = Str1 Then

MsgBox("you won")

'place the code you want here

End If

End If

Timer1.Enabled = True

End Sub



Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick

Flag1 = 1

Timer1.Enabled = False

End Sub

End Class





TexMav
Erik R
2013-05-03 22:23:48 UTC
Try to use the methods of the Strings class. (Contains, Substring, and Replace)

Here is some psuedo-code



***At Startup:

Create a constant string for Alphabet="abcdef...xyz" (entire alphabet)

Create your WordArray collection for the source words.



***Upon starting a new game

Create a string for AvailableChars = Alphabet constant from above

Create a string for CurrentWord and copy it from WordArray

Create a char[] the length of GuessedWord with empty chars in each cell



***Then

When the user enters a letter



if CurrentWord.Contains(enteredChar)

---loop the currentWord using currentWord.substring(loopCounter,1)

--and the that substring = the entered char, then put the enteredChar into

--the GuessedWord array at the loop position

---This allows the word to have multiple instances of the same letter.

ELSE

***It was a bad guess - do whatever you need to for that



Remove the enteredChar from the AvailableChars.Replace(enteredChar,'')



***Evaluate conditions

--Did the user lose the game?

--If not, send the GuessedWord array to a function that creates a string according to

the pattern of underscore if the array cell value is empty, or the cell value if not empty,

with a SPACE in between each cell for display purposes.



It is always best, in my opinion, to separate the needs of the UI from the functionality.

You don't want to maintain the state of the underscores and the spaces between them,

just generate a fresh display string each time.



Also the available chars list may be overkill, but it is nice if you were making a slightly

enhanced version to provide your user this information as well.
?
2013-05-03 21:31:54 UTC
There's several different ways to change "boat" (for example) to "_ _ _ _".



Here's one:



word_string$ = "boat"

length_of_word = LEN(word_string$)

for x = 1 to length_of_word

.....print "_"

next x





Just get the length of the word and then start a FOR NEXT loop, for as many letters that are in the word.



Yes, you need an array to store the letters that are guessed.



And yes, after a letter is guessed you need to loop through the letters in the word, looking for a match. It's easy.



guess$ = "t"

for x = 1 to length_of_word

.....letter_in_word$ = mid(letter_in_word$, x, 1)

.....if guess$ = letter_in_word$ then

.........print guess$

.....else

.........print "_"

.....end if

next x
?
2016-10-04 11:14:53 UTC
you are able to exchange the colors of area of a textual content string. go into Edit the string and hilite the characters you like, then use the toolbar to format those characters besides you elect. Then hit enter


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...