我写了一些代码。代码如下所示。第一部分是将 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/