Call the Replace Function. Change all instances of one substring to another and get a new String.
Replace. This changes the contents of a String. It makes all instances of one substring become another substring. The Replace function on String is often useful.
Function details. It is important to know some of this Function's details. Replace() cannot handle patterns or regular expressions. It can cause performance problems.
First example. Here we call Replace. Its parameters can be literal strings or dynamic ones determined at runtime. We replace the first word parameter with the second word parameter.
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
' Assign string to literal.
Dim value1 As String = "XXpagitica"
' Replace substring with another substring.
Dim value2 As String = value1.Replace("XX", "Areo")
Every instance. Replace() 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.
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
' 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")
Dot Net Perls is about Dot Net.
Dot Bottle Perls is about Dot Bottle.
Remove all substrings. We can use Replace() to remove all substrings from a string. A new copy of the string, with all matching substrings removed, is returned.String.Empty
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
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)
BEFORE: 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
However: The StringBuilder class implements optimizations that can improve memory usage and speed when doing many replacements.
VB.NET program that uses StringBuilder Replace
' Create new StringBuilder and initialize it.
Dim builder As New StringBuilder("This is an example.")
' Replace string in StringBuilder.
' Insert into StringBuilder.
builder.Insert(0, "Sentence: ")
This is an example.
Here is an example.
Sentence: Here is an example.
Internals. StringBuilder contains a mutable character buffer. Its methods can modify this buffer in-place, avoiding many allocations that would occur if you were using String.
Tip: If you have many replacements to perform, using StringBuilder will likely perform better.
Optimization. We can change code that uses the String type inefficiently into code that uses the StringBuilder type. This can greatly help performance.
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.
Regex. Using regular expressions and the Regex class in System.Text RegularExpressions namespace is often a better solution than the Replace method on string.Regex.Match
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.
A summary. The Replace function on String will allocate an entire new string. The StringBuilder's replacement method will change the internal buffer.
Replacements. These methods will loop through the string. They will replace all instances of the first parameter with the second parameter.