如何在VB.NET中替换子字符串?

问题描述 投票:0回答:1

我有一个涉及Visual Basic .NET中的字符串Replace函数的问题:

我的项目有一个Visual Basic脚本。我有一个名为RichTextBoxsample

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

上面的脚本返回一个空白文本框。请注意,文本框很丰富,包含非格式化但多行文本。我究竟做错了什么?

vb.net string text replace
1个回答
8
投票

字符串在.NET中是不可变的,Replace方法返回新值,它不会修改调用它的原始字符串。你需要重新分配它,如下所示:

mediacurrent = mediacurrent.Replace(string1, string2)
© www.soinside.com 2019 - 2024. All rights reserved.