修正版:
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”类型,变量声明也没有任何初始化语法。