【问题标题】:VisualBasic script : How to replace character at certain index in string?Visual Basic 脚本:如何替换字符串中某个索引处的字符?
【发布时间】:2014-07-22 18:43:32
【问题描述】:

考虑以下脚本

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

我想用前一个字符替换字符串“str”中的每个字符。

我正确地得到了 newChar,但是如何在知道 char 索引的情况下用它的 newChar 替换“str”中的每个 char?

【问题讨论】:

    标签: vbscript


    【解决方案1】:

    你正在使用Mid函数

    variable = Mid( string, start, length )
    

    AND Mid 指令

    Mid( target, start, length ) = string
    

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

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

    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
    

    【讨论】:

      猜你喜欢
      • 2014-03-31
      • 2012-02-15
      • 1970-01-01
      • 2011-10-20
      • 2020-12-24
      • 2020-07-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多