gpt4 book ai didi

arrays - vb6 声明一个数组

转载 作者:行者123 更新时间:2023-12-04 06:24:32 24 4
gpt4 key购买 nike

我有这段代码:

' Option Explicit
Public Function Clean(Text)
On Error Resume Next
' Dim Chars As ?????????
Chars = Array("\", "/", ":", "*", "?", """", "<", ">", "|")
For Each Replaced In Chars
Text = Replace(Text, Replaced, "")
Next
Clean = CStr(Text)
End Function

但是我在使用 Option Explicit 时出错,因为没有声明 Chars,但是我必须使用什么类型来使数组变暗 (Dim Chars As ???????? )?

最佳答案

更正版本:

Option Explicit

Public Function Clean(ByVal Text As String)
Dim Chars As Variant
Dim Replaced As Variant

Chars = Array("\", "/", ":", "*", "?", """", "<", ">", "|")
For Each Replaced In Chars
Text = Replace(Text, Replaced, "")
Next
Clean = Text
End Function

通常性能更好的版本:

Option Explicit

Public Function Clean(ByVal Text As String)
Dim Chars As Variant
Dim RepIndex As Long

Chars = Array("\", "/", ":", "*", "?", """", "<", ">", "|")
For RepIndex = 0 To UBound(Chars)
Text = Replace$(Text, Chars(RepIndex), "")
Next
Clean = Text
End Function

理解变体很重要,在使用字符串函数的变体版本而不是后缀为“$”类型装饰的字符串类型版本时应特别注意。

大多数时候,由于性能成本,您会希望尽可能避免变体。

这个版本可能表现得更好:

Option Explicit

Public Function Clean(ByVal Text As String)
Const Chars As String = "\/:*?""<>|"
Dim RepIndex As Long

For RepIndex = 1 To Len(Chars)
Text = Replace$(Text, Mid$(Chars, RepIndex, 1), "")
Next
Clean = Text
End Function

VB6中没有“Char”类型,也没有变量声明的初始化语法。

关于arrays - vb6 声明一个数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13860458/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com