修正版本:
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,也没有在声明变量的初始化语法。
@george:感谢编辑,它看起来好多了...... – faid