VB.NET Replace


Replace changes the contents of a String. It makes all instances of one substring become another substring. The Replace function on String is often useful. But it is important to know some of its details.


First, the Replace method on String object instances in VB.NET receives two parameters. The parameters can be literal strings embedded in your program or dynamic strings that are determined at runtime.

String type

When Replace is executed at runtime, all instances of the first parameter's string data are replaced with the second parameter's string data. You can use an empty string literal or the Nothing literal to indicate an empty string.

VB.NET program that replaces

Module Module1
    Sub Main()
	' Assign string to reference.
	Dim value1 As String = "Darth Vader is frightening."

	' Replace word with another word.
	Dim value2 As String = value1.Replace("frightening", "silly")
    End Sub
End Module


Darth Vader is frightening.
Darth Vader is silly.

In this example, we access the Replace method on the object's type pointer, replacing the first word parameter with the second word parameter. Finally, we write the resulting string data to the screen.

Every instance


The Replace method on the String type in the VB.NET language will internally loop through the entire source string. It replaces all occurrences of the substring you want to replace.

Tip:This behavior is useful when dealing with common string replacements in programs.

But:If you do not understand this behavior you may end up writing inefficient code that has unneeded loops.

VB.NET program that uses Replace on instances

Module Module1
    Sub Main()
	' Assign string to literal constant.
	Dim value1 As String = "Dot Net Perls is about Dot Net."

	' Replace every instance of the string.
	Dim value2 As String = value1.Replace("Net", "Bottle")
    End Sub
End Module


Dot Net Perls is about Dot Net.
Dot Bottle Perls is about Dot Bottle.

The string containing the characters "Net" is replaced with the characters "Bottle". These substrings are case-sensitive. They must occur in the same way as they are written in the program text in the target string.

Note:It is never useful to call the Replace method repeatedly with the same parameters.


Letters of the alphabet: ABC

The StringBuilder class has a Replace instance method that functions the same way as the String method. It also receives two string parameters that indicate the before and after substrings.

However:The StringBuilder class implements optimizations that can improve memory usage and speed when doing many replacements.

VB.NET program that uses StringBuilder Replace

Imports System.Text

Module Module1
    Sub Main()
	' Create new StringBuilder and initialize it.
	Dim builder As New StringBuilder("This is an example.")

	' Replace string in StringBuilder.
	builder.Replace("This", "Here")

	' Insert into StringBuilder.
	builder.Insert(0, "Sentence: ")
    End Sub
End Module


This is an example.
Here is an example.
Sentence: Here is an example.
Framework: NET

Internally, the StringBuilder class contains a mutable character buffer. This means that the StringBuilder's methods can modify this buffer in-place, avoiding many allocations that would occur if you were instead using String.

Tip:If you have many replacements to perform, using StringBuilder will likely perform better.



One common optimization you can make on existing programs is to change code that uses the String type inefficiently into code that uses the StringBuilder type. This can greatly help performance.

It is often useful to simply convert a string into a StringBuilder, and then use the Replace, Append and Insert methods on that StringBuilder. Then you can convert the StringBuilder back into a string with the ToString method.

StringBuilder Examples


Regex type

Using regular expressions and the Regex class in System.Text RegularExpressions namespace is often a better solution than the Replace method on string. Regular expressions allow you to do much more complex replacements with less code.

However:Simple replacements are faster to do in the String or StringBuilder classes. The regular expression parser has overhead.

Regex Type


We used the Replace methods on the String and StringBuilder type in the VB.NET language. The Replace method on string will allocate an entire new string. The StringBuilder's replacement method will change the internal buffer.

Review:Replace methods will loop through the string and replace all instances of the first parameter with the second parameter.

VB.NET: String