我有一个涉及Visual Basic .NET中的字符串Replace
函数的问题:
我的项目有一个Visual Basic脚本。我有一个名为RichTextBox
的sample
Dim string1 as string = "text to find"
Dim string2 as string = "text to replace find with"
Dim mediacurrent as string
mediacurrent = sample.text
mediacurrent.replace(string1, string2)
sample.text = mediacurrent
上面的脚本返回一个空白文本框。请注意,文本框很丰富,包含非格式化但多行文本。我究竟做错了什么?
字符串在.NET中是不可变的,Replace方法返回新值,它不会修改调用它的原始字符串。你需要重新分配它,如下所示:
mediacurrent = mediacurrent.Replace(string1, string2)