DIAdem Help

Method: Replace for VBS

  • Updated2024-09-12
  • 2 minute(s) read

Method: Replace for VBS

Replaces part of a text with another text.

vReplace = Object.Replace(expression, find, replacement, [start], [count], [compare])
ObjectVBS
Object with this method. You do not need to specify this object.
expressionVariant
Specifies the text you want to search through.
findVariant
Specifies the text to search for.
replacementVariant
Specifies the replacement text.
[start]Variant
Specifies the start position of the search. If you do not specify start, the search starts with the first character. If you specify count, you must also specify start.
[count]Variant
Specifies the number of replacements. If you do not specify count, the Replace method uses the value -1, which indicates that the method carries out all kinds of replacements.
[compare]Variant
Specifies the type of comparison when evaluating substrings. Possible settings are vbBinaryCompare and vbTextCompare. If you do not specify compare, the method performs a binary comparison.
vReplaceVariant
Receives the replaced text. The new text starts with the start position. Possible return values:
ConditionReturn value
expression is an empty stringEmpty string
expression is NullNull
find is an empty stringCopy of expression
start is greater than the length of expressionEmpty string

The following example replaces various texts in a text:

MyResult = Replace("ABC123abc", "abc", "def")
Call MsgBox(MyResult) 			' Returns ABC123def
MyResult = Replace("ABC123abc", "abc", "def", 1, -1, vbTextCompare)
Call MsgBox(MyResult) 			' Returns def123def
MyResult = Replace("ABC123abc", "abc", "def",4)
Call MsgBox(MyResult) 			' Returns 123def

Log in to get a better experience