Ntohing_Trolll Ntohing_Trolll - 21 days ago 6
Vb.net Question

Replacing text in a textbox, and then adding it to listbox

Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
If TextBox11.Text.Contains("https") Then
TextBox11.Text.Replace("https", "http")
Debug.WriteLineIf(TextBox11.Text.Contains("http"), "youtube link https replaced with http")
If TextBox11.Text.Contains("https") Then
ListBox3.Items.Add(TextBox11.Text)
Debug.WriteLine("items added to listbox")
End If
Else
Debug.WriteLine("items added to listbox(without repalce)")
ListBox3.Items.Add(TextBox11.Text)
End If

End Sub


so, what i tried to do here was to replace "https" with "https" in textbox11, and then adding it to listbox3, but,it not even replaces the text for some reason,and this is where i need a little bit of help.I know, stringbuilder is good for this,but i don't know how to use it, i only found how to replace a specified text, but not from a whole sentence.

p.s. sorry for my english.

Answer

The Replace method returns a new string with the replaced text. It doesn't work on the same string that you pass in. So you need to reassign the result of Replace

TextBox11.Text = TextBox11.Text.Replace("https", "http")