C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Argument 1: The first argument we pass to the Replace Function is the substring we want to change to something else.
Argument 2: This is the string we want to have in the result—the replacement value. This may be found in the result of Replace.
Info: All instances of the first parameter's string data are replaced with the second parameter's data.
VB.NET program that uses Replace
Module Module1
Sub Main()
' Assign string to literal.
Dim value1 As String = "XXpagitica"
Console.WriteLine(value1)
' Replace substring with another substring.
Dim value2 As String = value1.Replace("XX", "Areo")
Console.WriteLine(value2)
End Sub
End Module
Output
XXpagitica
Areopagitica
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.
Here: The string containing the characters "Net" is replaced with the characters "Bottle". These substrings are case-sensitive.
VB.NET program that uses Replace on instances
Module Module1
Sub Main()
' Assign string to literal constant.
Dim value1 As String = "The Dev Codes is about Dot Net."
Console.WriteLine(value1)
' Replace every instance of the string.
Dim value2 As String = value1.Replace("Net", "Bottle")
Console.WriteLine(value2)
End Sub
End Module
Output
The Dev Codes is about Dot Net.
Dot Bottle Perls is about Dot Bottle.
Important: A zero-length string (the empty string literal) is still a string, so we can replace substrings with it.
Nothing: We can use an empty string literal or the Nothing literal to indicate an empty string.
VB.NET program that uses Replace to remove substrings
Module Module1
Sub Main()
Dim value As String = "cat Remove dog Remove bird"
' We can use Replace to remove all instances of a substring.
Dim result As String = value.Replace("Remove", "")
Console.WriteLine("BEFORE: " + value)
Console.WriteLine("AFTER: " + result)
End Sub
End Module
Output
BEFORE: cat Remove dog Remove bird
AFTER: cat dog bird
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.")
Console.WriteLine(builder)
' Replace string in StringBuilder.
builder.Replace("This", "Here")
Console.WriteLine(builder)
' Insert into StringBuilder.
builder.Insert(0, "Sentence: ")
Console.WriteLine(builder)
End Sub
End Module
Output
This is an example.
Here is an example.
Sentence: Here is an example.
Tip: If you have many replacements to perform, using StringBuilder will likely perform better.
Tip: Often we can convert a string into a StringBuilder, and then use the Replace, Append and Insert methods on that StringBuilder.
Then: We can convert the StringBuilder back into a string with the ToString method.
Tip: 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.