||Searching for a substring and replacing it with a replacement substring can be a bit of a programmatic nightmare in some lower-level programming languages. Fortunately, this is not the case with VBScript. Instead, VBScript provides an extremely useful function called |
Replace that can be used replace all occurrences of a particular substring with a replacement substring.
Replace has the following definition:
Replace(expression, find, replaceWith[, start[, count[, compare]]])
expression parameter is the string to search through;
find is the substring to hunt for, and
replaceWith is the string to replace all occurrences of
find with. These are the only required parameters.
Replace returns a string that has all occurrences of the substring
find in the string
expression replaced with the substring
For example, if we wanted to replace all instaces of "cow" with "cat" in the following string:
The cow jumped over the moon.
we could do:
strSaying = "The cow jumped over the moon."
'Replace cow with cat
strSaying = Replace(strSaying, "cow", "cat")
'At this point, strSaying = "The cat jumped over the moon."
Replace also contains three optional parameters. The first,
count, determines the offset to start searching for
count determines how many total substitutions to perform. A value of -1 (the default) will cause all occurrences of
find to be replaced by
compare determines the comparison mode that should be used when searching for
vbBinaryCompare is specified then a case-sensitive search is performed; if
vbTextCompare is specified, then a case-insensitive search is performed. By default a case-sensitive search is performed.
For more information be sure to check out the
Replace technical docs!