Replace Function |
Microsoft.VisualBasic.Strings
Replace( expression, find, replace [, _ start [, count [, compare ]]])
The complete string containing the substring to be replaced
The substring to be found by the function
The new substring to replace find in expression
The character position in expression at which the search for find begins
The number of instances of find to replace
The method used to compare find with expression ; its value can be CompareMethod.Binary (for case-sensitive comparison) or CompareMethod.Text (for case-insensitive comparison)
The return value from Replace depends on the parameters you specify in the argument list, as the following table shows:
If | Return value |
---|---|
expression = "" | Zero-length string ("") |
find = "" | Copy of expression |
replace = "" | Copy of expression with all instances of find removed |
start > Len( expression ) | Zero-length string ("") |
count = 0 | Copy of expression |
Replaces a given number of instances of a specified substring in another string
If start is omitted, the search begins at the start of the string.
If count is omitted, all instances of the substring after start are replaced.
CompareMethod.BinaryCompare is case sensitive; that is, Replace matches both character and case, whereas CompareMethod.Text is case insensitive, matching only character regardless of case.
The default value for compare is CompareMethod.Binary .
start not only specifies where the search for stringToReplace begins, but also where the new string returned by the Replace function will commence.
If count is not used, be careful when replacing short strings that may form parts of unrelated words. For example, consider the following:
Dim sString sString = "You have to be careful when you do this " _ & "or you could ruin your string" Console.WriteLine(Replace(sString, "you", "we"))
Because we don't specify a value for count , the call to Replace replaces every occurrence of "you" in the original string with "we" . But the fourth occurrence of "you" is part of the word "your" , which is modified to become "wer" .
You must also be aware that if start is greater than 1, the returned string starts at that character and not at the first character of the original string, as you might expect. For example, given the statements:
sOld = "This string checks the Replace function" sNew = Replace(sOld, "check", "test", 5, _ CompareMethod.Text)
sNew will contain the value:
"string tests the Replace function"
You can use the Mid function on the left side of an argument to replace part of string, but to replace more than one instance of a substring requires a complicated Do While loop that constantly checks for the position of any remaining instances of the substring to be replaced.
The BCL's System.String class also has a public instance Replace method, which replaces all occurrences of a character or string with another. Its syntax is:
sString .Replace(oldValue, newValue )
where oldValue is a String or Char value containing the text to be replaced and newValue is a String or Char value containing the replacement text.
InStr Function, InStrRev Function, Mid Statement