VisualBasic 脚本:如何替换字符串中特定索引处的字符? [英] VisualBasic script : How to replace character at certain index in string?

查看:31
本文介绍了VisualBasic 脚本:如何替换字符串中特定索引处的字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑下面的脚本

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

我想用前一个字符替换字符串str"中的每个字符.

I want to replace every character in string "str" with it's previous one.

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

I get the newChar correctly but how do I replace each char from "str" with its newChar knowing the char index?

推荐答案

您正在使用 Mid 功能

variable = Mid( string, start, length )

AND Mid 指令

Mid( target, start, length ) = string

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

The Mid instruction (not function) is included in the list of elements in VB/VBA not included in VBS. So, you can not use it.

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

This is a similar solution to your code, but concatenation is used to generate the ouput string.

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

这篇关于VisualBasic 脚本:如何替换字符串中特定索引处的字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆