Visual Basic脚本:如何在字符串中的某个索引处替换字符?

时间:2014-07-22 10:09:03

标签: vbscript

考虑以下脚本

For i=1 To Len(str)
ascOfChar = Asc(Mid(str,i,1))  - 1
newChar = Chr(ascOfChar)
Mid(str,i,1) = newChar 

我想替换字符串中的每个字符" str"与它的前一个。

我正确地获得了newChar但是如何替换" str"中的每个char?其newChar知道char指数?

1 个答案:

答案 0 :(得分:0)

您正在使用Mid功能

variable = Mid( string, start, length )

Mid指令

Mid( target, start, length ) = string

Mid指令(非函数)包含在VBS中未包含的VB / VBA中的元素列表中。所以,你不能使用它。

这是与您的代码类似的解决方案,但连接用于生成输出字符串。

Option Explicit

    Dim str, output, i, newAsc

    str="This is a test 98765"
    output=""

    For i=1 To Len(str)
        newAsc = AscW(Mid(str,i,1))-1
        If newAsc < 0 Then 
            newAsc = 65535
        End If
        output = output & ChrW(newAsc)
    Next 

    WScript.Echo str
    WScript.Echo output
相关问题