Replace
This VB.NET function changes the contents of a String
. It makes all instances of one substring become another substring.
It is important to know some of this Function's details. Replace()
cannot handle patterns or regular expressions. It can cause performance problems.
Here we call Replace
. Its parameters can be literal strings or dynamic ones determined at runtime. We replace the first parameter with the second parameter.
Replace
Function is the substring we want to change to something else.string
we want to have in the result—the replacement value. This may be found in the result of Replace
.Module Module1 Sub Main() ' Replace substring with another substring. Dim value1 As String = "abcdef" Dim value2 As String = value1.Replace("abc", "hi") Console.WriteLine(value1) Console.WriteLine(value2) End Sub End Moduleabcdef hidef
Replace()
will internally loop through the entire source string
. It replaces all occurrences of the substring you want to replace.
string
replacements in programs.string
containing the characters "Net" is replaced with the characters "Bottle." These substrings are case-sensitive.Module Module1 Sub Main() ' Assign string to literal constant. Dim value1 As String = "Dot Net Perls 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 ModuleDot Net Perls is about Dot Net. Dot Bottle Perls is about Dot Bottle.
Remove
all substringsWe can use Replace()
to remove all substrings from a string
. A new copy of the string
, with all matching substrings removed, is returned.
string
(the empty string
literal) is still a string
, so we can replace substrings with it.string
literal or the Nothing literal to indicate an empty string
.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 ModuleBEFORE: cat Remove dog Remove bird AFTER: cat dog bird
StringBuilder
This class
has a Replace()
that functions the same way as the String
method. It has parameters that indicate the before and after substrings.
StringBuilder
class
implements optimizations that can improve memory usage and speed when doing many replacements.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 ModuleThis is an example. Here is an example. Sentence: Here is an example.
The Replace
function on String
will allocate an entire new string
. It will replace all instances of the first parameter with the second parameter.