vb.net - VB.net 中的字符串替换功能如何不起作用?

标签 vb.net

我写了一些代码。代码如下所示。第一部分是将 html 读取为字符串格式。第二部分是在字符串中搜索一个标记并将该字符串替换为其他字符串。

第一部分(我测试了很多次,效果很好)

Public Function ReadTextFile(ByVal TextFileName As String) As String
    Dim TempString As String
    Dim StreamToDisplay As StreamReader
    StreamToDisplay = New StreamReader(TextFileName)
    TempString = StreamToDisplay.ReadToEnd
    StreamToDisplay.Close()
    Return TempString
End Function

第二部分(我测试了很多次,搜索和替换不起作用。我检查了很多次“TempText”确实包含字符串。“the_key_string”确实在“TempText”字符串中。我通过使用 QuickWatch 来检查它VB.net。但是,替换功能不起作用)
            Dim TextPath = C:xxxxxx
            TempText = ReadTextFile(TextPath)
            TempText.Replace("the_key_string", "replace_by_this_string")

请帮忙。我不知道我在哪里犯了错误

最佳答案

String.Replace 返回新字符串而不是修改源字符串。您必须将其分配回您的变量:

TempText = TempText.Replace("the_key_string", "replace_by_this_string")

来自 MSDN:

Returns a new string in which all occurrences of a specified string in the current instance are replaced with another specified string.

关于vb.net - VB.net 中的字符串替换功能如何不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19234334/

相关文章:

.net - 如何选择使用VB.NET打开哪个Excel版本?

asp.net - 如何解决 ASP.NET、VB.NET 中的 session 问题?

json - 无法将当前 JSON 对象(例如 {"name":"value"})反序列化为类型 'System.Collections.Generic.List`

asp.net - GridView 中具有列跨度的标题列

c# - : Conversion operators cannot convert from an interface type如何解决

mysql - 在.NET中获取MySQL数据时出错

c# - 弹道库

mysql - 在VB.Net中的标签上显示数据库记录

mysql - 在VB MySQL编程中动态提供列名

vb.net - 隐藏 NumericUpDown 控件上的向上/向下按钮