返回一个字符串,其中的指定子字符串已被另一个子字符串替换指定次数。
语法
Replace(expression, find, replacewith[, start[, count[, compare]]])
参数:
表 11-19 比较常量和说明
| 常量 | 值 | 说明 |
|---|---|---|
vbBinaryCompare |
0 | 执行二进制比较 |
vbTextCompare |
1 | 执行文本比较 |
返回值
Replace 将返回以下值:
表 11-20 Replace 返回值
| 如果 | Replace 返回 |
|---|---|
| expression 为零长度 | 零长度字符串 ("") |
| expression 为 Null | 错误 |
| find 为零长度 | expression 的副本 |
| replacewith 为零长度 | expression 的副本(删除出现的所有 find) |
start > Len (expression) |
零长度字符串 |
| count 为 0 | expression 的副本 |
注释
Replace 函数的返回值是一个字符串,其中进行了替换,该字符串从 start 指定的位置开始,并在 expression 字符串的末尾结束。它不是原始字符串从头到尾的副本。
以下示例使用 Replace 函数返回字符串:
示例 1:
' A binary comparison starting at the beginning of the string.
Dim MyString
MyString = Replace("XXpXXPXXp", "p", "Y")
' Output: " XXYXXPXXY".
示例 2:
' A textual comparison starting at position 4.
Dim MyString
MyString = Replace("XXpXXPXXp", "p", "Y", 4)
'Output: XXPXXY
示例 3:
Dim MyString
MyString = Replace("XXpXXPXXp", "X", "Y", 1, 4)
'Output: YYpYYPXXp
示例 4:
Dim MyString
MyString = Replace("XxpXxPXxp", "X", "Y", 1, -1, 0)
'Output: YxpYxPYxp
MyString = Replace("XxpXxPXxp", "X", "Y", 1, -1, 1)
'Output: YYpYYPYYp
示例 5:
Dim MyString2
MyString2 = Replace("XXpXXPXXp", "p", "")
' Output: "XXXXPXX".
示例 6:
Dim MyString4
MyString4 = Replace("XXpXXPXXp", "p", "Y", 1, 0)
' Output: "XXpXXPXXp".
示例 7:
Dim MyString5
MyString5 = Replace("XXpXXPXXp", "", "Y")
'Output: "XXpXXPXXp".