0

I have the code to replace the string of the predetermined variable, but it seems my code is not efficient, because if more strings that want to be replace, the more replace functions, how do I handle this?

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

Thanks for help

Abu Ayyub
  • 401
  • 4
  • 14
  • `desc = appName & " " & appVer & " is free program"`? – GSerg Jan 31 '20 at 18:00
  • actually i will use multilanguage for my "desc" string, is that possible to convert replace into custom function? – Abu Ayyub Jan 31 '20 at 18:03
  • 1
    Does this answer your question? [Implementing String.Format() in VB6](https://stackoverflow.com/questions/14534360/implementing-string-format-in-vb6) – GSerg Jan 31 '20 at 18:06

1 Answers1

1

I answer my question

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

Hope this will help other in same case

Abu Ayyub
  • 401
  • 4
  • 14
  • 1
    Clever approach, I think, though also a little bit 'hidden' in how it works. – StayOnTarget Feb 03 '20 at 19:29
  • this will use in my multilang app, which is the lang strings store in res file, so just use "{appVersion} or {appName} or others to replace it with dynamic values" – Abu Ayyub Feb 03 '20 at 20:49