Question Locked

Replacing a full string in VB.net

0 helpful
5 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

I have a hangman project to do for school and I can't figure out how to parse through the whole string and replace it with a character like " * "

I wanna replace every character in the word with "*". So far I only replaced the whole word with 1 character. There is some kind of forloop that I need and can't seem to figure it out. Here is an example of what I have:

Dim OldText As String
Dim NewText As String
Dim letter As String = "*"

OldText = Me.lblA.Text
NewText = OldText.Replace(OldText, letter)
Me.lblA.Text = NewText

My lblA is connected to an access database where it randomly displays a question and an answer, but I want to hide the answer since it's hangman.

Any help?

5 replies

by tony hopkinson

you want to hide so
Use the StringBuilder class
There is a method called append pass in '*' and the length of the input string, in C#

Public String Obscure(String input)
{
StringBuilder sb = new StringBuilder(input.Length);
sb.Append('*',input.Length);
return sb.ToString();
}

by b.wong

Dim oRead As System.IO.StreamReader
Dim oWrite As System.IO.StreamWriter
Dim str As String
oRead = New StreamReader(New FileStream(filename, FileMode.OpenOrCreate), Encoding.Default)
str = oRead.ReadToEnd
While dr.Read
str = str.Replace("FROM","TO")
End While
oRead.Close()
oRead = Nothing
dr.Close()
oWrite = New StreamWriter(New FileStream(filename, FileMode.Create), Encoding.Default)
oWrite.Write(str)
oWrite.Close()

by polishthug

The piece of code I got here finds a letter and replaces it but it is not looped therefore it stops and doesn't go on. I've been trying to figure out the right DO WHILE loop for this and couldn't get it.

Here is my replace code:

Dim guess As String
Dim loc As Integer
Dim hidden() As Char

hidden =Me.lblAnswer.Text.ToLower
guess =Me.txtGuess.Text.ToLower
loc = answer.IndexOf(guess)

If answer.IndexOf(guess) > -1 Then

MessageBox.Show("Got one!")
hidden(loc) = guess(0)
Me.txtGuess.Focus()
Me.lblAnswer.Text = hidden

End If

Do you guys have any suggestions? I'm thinking of something like
Do While answer.indexof(guess,loc+1)

but that has failed me.. any help would be appreciated.

by tony hopkinson

wouldn't it?
Assuming displayed answer starts as a string of '*'
for j = 1 to length(answer)
if answer[j] = guesschar then
displaydanswer[j] = guesschar

or some such

The reason you can't get your ideas to work is you are using simple strings as your data structures.

Sneaky type solution
Answer = "HANGMAN"

DisplayedAnswer = Answer.ToLower().
initially

CurrentAnswerteextBox.Text = HideAnswer()

Hide answer converts any lower case char to a *
Then when you get a guess, convert it to lower
then replace all instances of it with upper.

Hideanswer no longer hides it...

This discussion is closed to new replies.