使用replace关键字删除字符串

时间:2017-12-19 02:17:07

标签: c# replace

我遇到String.Replace方法的问题。它不适用于buttonclick事件。

我使用此代码

private void button4_Click(object sender, EventArgs e)
{
       textBox3.Text.Replace("apple","");
}

Please see the image

当我按下清除按钮时,它不会删除" apple"字

1 个答案:

答案 0 :(得分:2)

替换功能不会更改控件,它会返回一个新字符串。

如果textBox3说" applebananacherry"

String text = textBox3.Text.Replace("apple","");

会留下文字" bananacherry"

您需要的是

textBox3.Text = textBox3.Text.Replace("apple", "");