我有代码来替换预定变量的字符串,但似乎我的代码效率不高,因为如果要替换的字符串越多,替换函数就越多,我该如何处理?
Dim appName As String
Dim appVer As String
Dim desc As String
appName = "MyProject"
appVer = App.Major & "." & App.Minor & "." & App.Revision
desc = "{appName} {appVer} is free program"
desc = Replace(desc, "{appName}", appName)
desc = Replace(desc, "{appVer}", appVer)
Label1.Caption = desc
感谢您的帮助
我回答我的问题
Public Function ReplaceString(sString As String) As String
Const Tag1 = "{"
Const Tag2 = "}"
Dim sItem() As String, i As Long
sString = Replace(sString, "n", vbNewLine) 'Replace new line
sItem = Split(sString, Tag1)
For i = 1 To UBound(sItem)
sItem(i - 1) = Split(sItem(i), Tag2, 2)(0)
Next
ReDim Preserve sItem(UBound(sItem) - 1)
For i = 0 To UBound(sItem)
sString = Replace(sString, "{" & sItem(i) & "}", CallByName(Me, sItem(i), VbGet))
Next
ReplaceString = sString
End Function
希望这将在相同情况下对其他人有所帮助