【问题标题】:How to replace a substring in VB.NET?如何在 VB.NET 中替换子字符串?
【发布时间】:2011-09-03 09:12:19
【问题描述】:

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

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

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】:

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

    mediacurrent = mediacurrent.Replace(string1, string2)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-18
      • 1970-01-01
      • 2016-09-29
      • 2014-06-09
      • 2015-11-13
      • 2020-04-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多