-
Aug 14th, 2018, 10:24 AM
#1
Thread Starter
Addicted Member
String substitutions with variables in vb6
Hello do we have in vb6 something more simple to do this:
Code:
myNumber = 6
myProduct = "Table"
myString = "This is my %i try to use this %j in my life"
myString = Replace (myString, "%i", myNumber)
myString = Replace (myString, "%j", myProduct)
and now to replace the %i and %j with the values from the variables or is this the only way?
P.S.
this is also not the best solution:
Code:
myNumber = 6
myProduct = "Table"
myString = "This is my " & myNumber & " try to use this " & myProduct & " in my life"
I would like to implement multilanguage in my app so that I have one string for this in my replacement language file
Last edited by Davor Geci; Aug 14th, 2018 at 10:28 AM.
My projects:
Virtual Forms
VBA Telemetry
-
Aug 14th, 2018, 11:21 AM
#2
Re: String substitutions with variables in vb6
I use an Expand() function, that basically does {token} substitution, with provided source data.
The source data is typically a collection, a recordset object or even environment variables.
But usually this snippet is sufficient.
Code:
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Function Fmt$(Format As String, ParamArray Args())
' Minimal Sequential {Token#} Replacing ie: {0} {1}
' Replaces '\n' With vbNewLine
' Replaces '\t' With vbTab
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Fmt = Replace(Format, "\n", vbNewLine)
Fmt = Replace(Fmt, "\t", vbTab)
Dim Arg As Long
For Arg = 0 To UBound(Args)
Fmt = Replace(Fmt, "{" & Arg & "}", Nz(Args(Arg)))
Next
End Function
So
Code:
myString = Fmt("This is my {0} try to use this {1} in my life", myNumber, myProduct)
Last edited by DEXWERX; Aug 14th, 2018 at 11:26 AM.
Reason: collection not connection...
-
Aug 14th, 2018, 11:57 AM
#3
Re: String substitutions with variables in vb6
Seems a bit odd, would you not need to replace the entire string if you are doing multiple languages? Seems that replacing just a word here and there would not be of much use.
-
Aug 14th, 2018, 12:06 PM
#4
Re: String substitutions with variables in vb6
You can also use IVBPrint interface and just use Class.Print "This is my " ; myNumber ; " try to use this " ; myProduct ; " in my life", like usually print.
-
Aug 14th, 2018, 12:09 PM
#5
Re: String substitutions with variables in vb6
Originally Posted by DataMiser
Seems a bit odd, would you not need to replace the entire string if you are doing multiple languages? Seems that replacing just a word here and there would not be of much use.
yes.. the whole string _is_ replaced from a string table resource. The token replacement is only tangentially related to using string resources in general.
string resources are stored with the embedded tokens.... -_-
Last edited by DEXWERX; Aug 14th, 2018 at 02:54 PM.
-
Aug 14th, 2018, 02:21 PM
#6
Re: String substitutions with variables in vb6
Hi,
he one more option..
Code:
Option Explicit
Private Tb() As String
Private Sub Command1_Click()
Text2.Text = ConvertText(Text1.Text, Tb())
End Sub
Private Sub Form_Load()
Dim i As Long
'Tabelle initialisieren
ReDim Tb(255)
'Ansi Werte 1:1 zuweisen
'A=A, a=a etc.
For i = 0 To UBound(Tb)
Tb(i) = Chr(i)
Next
'Test:
Tb(Asc("h")) = "Green"
Tb(Asc("a")) = "(5000)"
Tb(Asc("b")) = "(5005)"
Tb(Asc("c")) = "(5010)"
Tb(Asc("Z")) = "Y"
'Textbox vorbesetzen
Text1.Text = "habcZ"
Text2.Text = vbNullString
End Sub
'------------------------------------------------------------
'einen Text konvertieren nach einer vorbelegten CodeTabelle
'die Zeichenlänge in der Codetabelle ist variabel von 1 bis n
'------------------------------------------------------------
Public Function ConvertText(sText As String, _
CodeTb() As String) As String
Dim i As Long, j As Long, z As Long
Dim Buffer As String
'Buffer dimensionieren
Buffer = Space(128)
'zum Test nur mit 2 Spaces
Buffer = Space(2)
'in Schleife Text Zeichen für Zeichen abarbeiten
For i = 1 To Len(sText)
'Dezimalwert des nächsten Zeichens
z = Asc(Mid$(sText, i, 1))
'Buffer überprüfen
Do
'passt der umgesetzte Code noch rein
If (j + Len(CodeTb(z))) <= Len(Buffer) Then
'ja, keine Anpassung notwendig
Exit Do
End If
'Buffer vergrössern um 50%
Buffer = Buffer & Space(Len(Buffer) / 2)
Loop
'Zeichen übersetzen & einfügen
Mid$(Buffer, j + 1) = CodeTb(z)
'belegte Länge im Buffer aktualisieren
j = j + Len(CodeTb(z))
Next
'nur belegten Teil des Buffers als Ergebnis liefern
ConvertText = Left$(Buffer, j)
End Function
regards
Chris
to hunt a species to extinction is not logical !
since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.
-
Aug 14th, 2018, 03:08 PM
#7
Re: String substitutions with variables in vb6
I'm using something similar to DEXWERX's function:
thinBasic Code:
Public Function Printf(ByVal sText As String, ParamArray A() As Variant) As String Const LNG_PRIVATE As Long = &HE1B6 '-- U+E000 to U+F8FF - Private Use Area (PUA) Dim lIdx As Long For lIdx = UBound(A) To LBound(A) Step -1 sText = Replace(sText, "%" & (lIdx - LBound(A) + 1), Replace(A(lIdx), "%", ChrW$(LNG_PRIVATE))) Next Printf = Replace(sText, ChrW$(LNG_PRIVATE), "%") End Function
This would be used like this in your case:
myString = Printf("This is my %1 try to use this %2 in my life", myNumber, myProduct)
i.e. %1 is expanded with first arg, %2 with second, etc.
The function is longer as it takes care when the actual value in myNumber contains %2 *not* to replace this placeholder with myProduct in the second itertion of the replacement loop.
Anyway, this seems good enough for short strings and if performance is consideration and/or dealing with larger texts then you can investigate regular expressions and string builders to achieve equivalent results with less string allocations.
cheers,
</wqw>
-
Aug 15th, 2018, 05:07 AM
#8
Re: String substitutions with variables in vb6
You could make one pass to determine the expanded length, then another to plop things into an allocated String via Mid$() statements:
Code:
Option Explicit
Private Function Expand(ByRef Template As String, ParamArray Args() As Variant) As String
'Insertions %0 through %9, escape literal % as %%.
Const ASC0 As Long = &H30& 'AscW("0").
Dim I As Long
Dim TemplPos As Long
Dim Escaped As String
Dim Expansion As Long
Dim NewPos As Long
Dim OutPos As Long
Dim Text As String
If 0 > UBound(Args) Or UBound(Args) > 9 Then
Err.Raise 5 'Invalid procedure call or argument.
End If
For I = 0 To UBound(Args)
Args(I) = CStr(Args(I))
Next
Do
TemplPos = InStr(TemplPos + 1, Template, "%")
If TemplPos > 0 Then
TemplPos = TemplPos + 1
Escaped = Mid$(Template, TemplPos, 1)
If Escaped = "%" Then
Expansion = Expansion - 1
Else
If "0" > Escaped Or Escaped > "9" Then
Err.Raise 5 'Invalid procedure call or argument.
End If
Expansion = Expansion + (Len(Args(AscW(Escaped) - ASC0)) - 2)
End If
End If
Loop While TemplPos > 0
Expand = Space$(Len(Template) + Expansion)
OutPos = 1
Do
NewPos = InStr(TemplPos + 1, Template, "%")
If NewPos > 0 Then
Mid$(Expand, OutPos) = Mid$(Template, TemplPos + 1, NewPos - TemplPos - 1)
OutPos = OutPos + NewPos - TemplPos - 1
NewPos = NewPos + 1
Escaped = Mid$(Template, NewPos, 1)
If Escaped = "%" Then
Mid$(Expand, OutPos) = "%"
OutPos = OutPos + 1
Else
Text = Args(AscW(Escaped) - ASC0)
Mid$(Expand, OutPos) = Text
OutPos = OutPos + Len(Text)
End If
Else
Mid$(Expand, OutPos) = Mid$(Template, TemplPos + 1)
End If
TemplPos = NewPos
Loop While TemplPos > 0
End Function
Private Sub Form_Load()
AutoRedraw = True
Print "Hi %0."
Print "["; Expand("Hi %0.", "Joe"); "]"
Print
Print "Please send $%2 to %0 %1. %0 needs it now."
Print "["; _
Expand("Please send $%2 to %0 %1. %0 needs it now.", _
"Joe", _
"Veeblefester", _
150@); _
"]"
Print
Print "Only %0%% per month, act now!"
Print "["; Expand("Only %0%% per month, act now!", 2.35); "]"
End Sub
-
Aug 15th, 2018, 05:34 AM
#9
Thread Starter
Addicted Member
Re: String substitutions with variables in vb6
Yes, guys this is just the thing that I wanted.
Thanks to all of you. I will test your solutions and see how this impact to the speed also.
Davor
My projects:
Virtual Forms
VBA Telemetry
-
Aug 15th, 2018, 06:12 AM
#10
Re: String substitutions with variables in vb6
thinBasic Code:
Public Function PrintfEx(ByVal sText As String, ParamArray A() As Variant) As String Static oRegExp As Object Dim lSize As Long Dim lIdx As Long Dim lPrevIndex As Long Dim lPos As Long Dim sValue As String If oRegExp Is Nothing Then Set oRegExp = CreateObject("VBScript.RegExp") oRegExp.Global = True oRegExp.Pattern = "%\d+" End If lSize = Len(sText) With oRegExp.Execute(sText) For lIdx = 0 To .Count - 1 With .Item(lIdx) sValue = A(Mid$(.Value, 2) - 1) lSize = lSize - .Length + Len(sValue) End With Next PrintfEx = String$(lSize, 0) lPos = 1 For lIdx = 0 To .Count - 1 With .Item(lIdx) lSize = .FirstIndex - lPrevIndex Mid$(PrintfEx, lPos, lSize) = Mid$(sText, lPrevIndex + 1, lSize) lPrevIndex = .FirstIndex + .Length sValue = A(Mid$(.Value, 2) - 1) Mid$(PrintfEx, lPos + lSize, Len(sValue)) = sValue lPos = lPos + lSize + Len(sValue) End With Next lSize = Len(sText) - lPrevIndex If lSize > 0 Then Mid$(PrintfEx, lPos, lSize) = Mid$(sText, lPrevIndex + 1, lSize) End If End With End Function
Same as dilettante's in priciple (single output string allocation) but based on RegExp. First calculates output string length, then in-place retrofits replacements.
This one handles %NUM for any NUM, NUM is 1-based and % needs not be escaped.
cheers,
</wqw>
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|