替换文本框中的文本,然后将其添加到列表框中

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

所以,我在这里尝试做的是在 textbox11 中用 "https" 替换 "https",然后将其添加到 listbox3,但是,由于某种原因它甚至没有替换文本,并且这是我需要一点 help.I 知道的地方,stringbuilder 很适合这个,但我不知道如何使用它,我只找到了如何替换指定的文本,但不是整个句子。

p.s。对不起我的英语。

Replace 方法returns 一个新字符串替换文本。它不适用于您传入的相同字符串。因此您需要重新分配 Replace

的结果
TextBox11.Text = TextBox11.Text.Replace("https", "http")

我建议您使用以下代码(为了便于阅读省略了 Debug 个子句):

Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
    If TextBox11.Text.ToLower.Contains("https") Then
        TextBox11.Text = TextBox11.Text.ToLower.Replace("https", "http")
    End If
    ListBox3.Items.Add(TextBox11.Text.ToLower)
End Sub

让我们传播一下代码更改:

  • ToLower 方法确保用户没有使用大写字母输入值。
  • TextBox11.Text = TextBox11.Text.ToLower.Replace("https", "http") 是为 TextBox 对象分配更正值的正确方法。
  • If...End If 结构的变化是可以理解的 - 无论 TextBox 值是否已更正,您都将填充 ListBox 对象。