Skip to content
本页内容

Replace (方法)

根据指定的字符数,将文本字符串的部分用其他文本字符串替换。返回值是替换之后的新字符串。

参数

属性数据类型必填说明
Arg1str必填要替换其中某些字符的文本。
Arg2float必填Arg1 中用 Arg4 替换的字符的位置。
Arg3float必填Arg1 中使用 Replace 方法替换为 Arg4 的字符个数。
Arg4str必填用以替换 Arg1 中字符的文本。

返回值

str

示例

python
#此示例用“ac-ef”替换“abcdef”,并在该过程中通知用户
def test():
    strCurrent = "abcdef"
    # Notify user and display current string.
    print("The current string is:" + strCurrent)
    #Replace "cd" with "-".
    strReplaced = Application.WorksheetFunction.Replace (strCurrent, 3, 2, "-")
    # Notify user and display replaced string.
    print ("The replaced string is:" + strReplaced)
python
#本示例为A1等单元格赋值,使用 Replace 方法根据指定的字符数将文本字符串的一部分替换为其他文本字符串
def test():
    Range("A1").Value2 = "欢迎你加入WPS大家庭!"
    Range("A2").Value2 = 6
    Range("A3").Value2 = 3
    Range("A4").Value2 = "金山办公"
    print(WorksheetFunction.Replace(Range("A1").Value2, Range("A2").Value2, Range("A3").Value2, Range("A4").Value2))