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

3
  • desc = appName & " " & appVer & " is free program"? Commented Jan 31, 2020 at 18:00
  • actually i will use multilanguage for my "desc" string, is that possible to convert replace into custom function? Commented Jan 31, 2020 at 18:03
  • 1
    Does this answer your question? Implementing String.Format() in VB6 Commented Jan 31, 2020 at 18:06

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

2 Comments

Clever approach, I think, though also a little bit 'hidden' in how it works.
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"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.