Question Finding and replacing words from a textbox

tomg990

New member
Joined
May 17, 2013
Messages
2
Programming Experience
Beginner
Hi, I'm trying to find certain words from a textbox and replace them with blank space, this is what I have at the moment: TextBox1.Text = TextBox1.Text.Replace(ReplaceString1, "")

TextBox1.Text = TextBox1.Text.Replace(ReplaceString2, "")

TextBox1.Text = TextBox1.Text.Replace(ReplaceString3, "")
The problem is, it is letters of the strings aswell, for example if the ReplaceString1's contents was 'it', then after the replace a word such as hit would read 'h'. Is there any way to stop this happening, and replace these strings ONLY if they are surrounded by white space?
 
Last edited:
You wrote the solution yourself, just look for the word surrounded by white spaces.

TextBox1.Text = TextBox1.Text.Replace(" " & ReplaceString & " ", "")
 
How is this piece of code working, from what I can see it is just going to replace what ever is in the text box with a "Space" what ever the replacestring value is and "Space".

I don't understand what the , "" is for. Please explain.

**UPDATE**

Public Class Form1
Dim ReplaceString As String = "Herman for the win"
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
TextBox1.Text = TextBox1.Text.Replace(" " & ReplaceString & " ", "")
End Sub
End Class

This does nothing :(
 
Last edited:
How is this piece of code working, from what I can see it is just going to replace what ever is in the text box with a "Space" what ever the replacestring value is and "Space".

I don't understand what the , "" is for. Please explain.

**UPDATE**

Public Class Form1
Dim ReplaceString As String = "Herman for the win"
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
TextBox1.Text = TextBox1.Text.Replace(" " & ReplaceString & " ", "")
End Sub
End Class

This does nothing :(

The "" is an empty string. The code is saying replace the first substring with the second and the second is an empty string so it's basically saying remove the first substring. Using your example, if the TextBox initially contained "ABC Herman for the win XYZ" then it would end up containing "ABCXYZ" because the highlighted part is what is being replaced with no text.
 
Back
Top