【问题标题】:Replace string into define variable in VB6在VB6中将字符串替换为定义变量
【发布时间】:2020-02-01 04:46:23
【问题描述】:

我有替换预定变量的字符串的代码,但是我的代码似乎效率不高,因为如果要替换的字符串越多,替换函数越多,我该如何处理?

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

感谢您的帮助

【问题讨论】:

  • desc = appName & " " & appVer & " is free program"?
  • 实际上我会为我的“desc”字符串使用多语言,是否可以将替换转换为自定义函数?
  • 这能回答你的问题吗? Implementing String.Format() in VB6

标签: vb6


【解决方案1】:

我回答我的问题

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

希望这对同样情况下的其他人有所帮助

【讨论】:

  • 我认为这是一种聪明的方法,尽管它的工作方式也有点“隐藏”。
  • 这将在我的多语言应用程序中使用,这是存储在 res 文件中的语言字符串,因此只需使用“{appVersion} 或 {appName} 或其他将其替换为动态值”
猜你喜欢
  • 1970-01-01
  • 2015-04-18
  • 2013-02-16
  • 1970-01-01
  • 2020-07-09
  • 1970-01-01
  • 2020-02-04
  • 1970-01-01
相关资源
最近更新 更多