Marco
Marco

Reputation: 1025

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

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

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

Upvotes: 9

Views: 47840

Answers (8)

spinjector
spinjector

Reputation: 3535

something came to my mind as I was reading this. As readers should understand after reading this post, String.Replace is a function, not a statement. However, StringBuilder.Replace() is a statement.

Thus, to use the poster's code snippet as a model, the following would work...

    Dim sb As System.Text.StringBuilder
    sb = New System.Text.StringBuilder("Hello, the_key_string!")
    sb.Replace("the_key_string", "replace_by_this_string")
    MsgBox(sb.ToString)

Also, depending on circumstances, StringBuilder may be more efficient & faster than String.

Upvotes: 0

R2D2
R2D2

Reputation: 71

try these and one might work. i think that it is the underscore.

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

TempText.Replace("the key string", "replace_by_this_string")

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

Upvotes: 0

Tim Schmelter
Tim Schmelter

Reputation: 460340

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

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.

Upvotes: 5

int3
int3

Reputation: 658

"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

Upvotes: 0

ChrisW
ChrisW

Reputation: 9409

The Replace method returns the modified string.

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")

Upvotes: 0

David
David

Reputation: 219087

This is performing the string replace, but it's not putting the result of 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")

Upvotes: 2

Jonathan Bates
Jonathan Bates

Reputation: 1835

You have to assign the value to something, like :

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

Upvotes: 2

MarcinJuraszek
MarcinJuraszek

Reputation: 125650

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")

From MSDN:

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

Upvotes: 15

Related Questions