VB.net 中的字符串替换功能如何不起作用? [英] How does the string replace function in VB.net not work?

查看:25
本文介绍了VB.net 中的字符串替换功能如何不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I wrote up some code. The code is shown below. The first part is to read a html into string format. The second part is to search a mark in the string and replace the string by other string.

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

The 1st part (I test it many times, it works fine)

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"字符串中.我通过在 VB.net 中使用 QuickWatch.但是,替换功能不起作用)

The 2nd part (I test it many times, the search and replace does not work. I checked many times that the "TempText" DOES contain string. The "the_key_string" DOES inside the "TempText" String. I check it by using QuickWatch in VB.net. However, the replace function does NOT do its job)

            Dim TextPath = C:xxxxxx
            TempText = ReadTextFile(TextPath)
            TempText.Replace("the_key_string", "replace_by_this_string")

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

Please help. I have no clue where I made the mistake

推荐答案

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

String.Replace returns new string instead of modifying the source one. You have to assign it back to your variable:

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 中的字符串替换功能如何不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆