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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19234334/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 17:12:49  来源:igfitidea点击:

How does the string replace function in VB.net not work?

vb.net

提问by Marco

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.

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

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

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)

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

            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

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

回答by MarcinJuraszek

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

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

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

From MSDN:

来自 MSDN:

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

返回一个新字符串,其中当前实例中所有出现的指定字符串都替换为另一个指定字符串。

回答by Tim Schmelter

Strings are immutable, that means once they are created you cannot modify them. So you have to create a new one and assign that to your string variable:

字符串是不可变的,这意味着一旦它们被创建,你就不能修改它们。因此,您必须创建一个新的并将其分配给您的字符串变量:

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

MSDN: String Data Type (Visual Basic):

MSDN: String Data Type (Visual Basic):

Once you assign a string to a String variable, that string is immutable, which means you cannot change its length or contents. When you alter a string in any way, Visual Basic creates a new string and abandons the previous one. The String variable then points to the new string.

将字符串分配给 String 变量后,该字符串是不可变的,这意味着您无法更改其长度或内容。当您以任何方式更改字符串时,Visual Basic 会创建一个新字符串并放弃前一个字符串。然后 String 变量指向新的字符串。

回答by ChrisW

The Replace method returns the modified string.

Replace 方法返回修改后的字符串。

You need something like this:

你需要这样的东西:

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

回答by int3

"this is a string" If you do Replace 'string' with 'whatever' this string should be: "this is a whatever". so what you can do is put that in a new string. how? replace method returns a string so, it is easy :) see this: msdn

“这是一个字符串”如果你用“随便”替换“字符串”,这个字符串应该是:“这是一个随便的”。所以你可以做的是把它放在一个新的字符串中。如何?replace 方法返回一个字符串,所以很容易:) 看这个:msdn

回答by Jonathan Bates

You have to assign the value to something, like :

您必须将值分配给某些东西,例如:

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

回答by David

This is performing the string replace, but it's not putting the resultof it anywhere:

这是执行字符串替换,但它没有把它的结果放在任何地方:

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

You need to assign the result to something:

您需要将结果分配给某些东西:

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