Last active
April 27, 2023 12:34
-
-
Save codeartery/8bed157ff8ede837ee423c9ab3ce9562 to your computer and use it in GitHub Desktop.
Evaluates data between { brackets } or user defined tokens, within strings, and allows for easy string appending.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Class FormatString | |
REM@description | |
' Evaluates data between { brackets } or user defined tokens, within strings, and allows for easy string appending. | |
REM@author | |
' Jeremy England, http://codeartery.com/ | |
REM@mini | |
' Class FormatString:Private l,r,n,q:Sub Class_Initialize:Tokens="{}":n=vbLf:q="""":End Sub:Property Let Tokens(s):l=Left(s,1):r=Right(s,1):End Property:Property Let Append(f,s):f=f&Format(s):End Property:Public Default Function Format(s):Dim p,d:Format=s:p=InStr(1,Format,l)+1:If(p=1)Then:Exit Function:End If:d=Mid(Format,p,InStr(p,Format,r)-p):Format=Format(Replace(Format,(l&d&r),Eval(d))):End Function:End Class | |
Private tokenL, tokenR, n, q | |
Sub Class_Initialize | |
Tokens = "{}" | |
n = vbLf | |
q = """" | |
End Sub | |
Property Let Tokens( str ) | |
tokenL = Left( str, 1 ) | |
tokenR = Right( str, 1 ) | |
End Property | |
Property Let Append( ByRef ref, str ) | |
ref = ref & Format( str ) | |
End Property | |
Public Default Function Format( str ) | |
Dim tPos, tData | |
Format = str | |
tPos = InStr( 1, Format, tokenL ) + 1 | |
If( tPos = 1 )Then Exit Function | |
tData = Mid( Format, tPos, InStr( tPos, Format, tokenR ) - tPos ) | |
Format = Format( Replace( Format, (tokenL & tData & tokenR), Eval(tData) ) ) | |
End Function | |
End Class |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
REM@usage | |
' Put the full or mini class/sub/function in your script to use. | |
Class FormatString:Private l,r,n,q:Sub Class_Initialize:Tokens="{}":n=vbLf:q="""":End Sub:Property Let Tokens(s):l=Left(s,1):r=Right(s,1):End Property:Property Let Append(f,s):f=f&Format(s):End Property:Public Default Function Format(s):Dim p,d:Format=s:p=InStr(1,Format,l)+1:If(p=1)Then:Exit Function:End If:d=Mid(Format,p,InStr(p,Format,r)-p):Format=Format(Replace(Format,(l&d&r),Eval(d))):End Function:End Class | |
Dim F : Set F = New FormatString | |
Dim name, age | |
name = "Jeremy" | |
age = 97 | |
msgbox F.Format("Welcome to the FormatString Class.") | |
Dim msg, title | |
title = F("Hello {name}. Happy {age}th birthday!") | |
F.Append(msg) = "{name} is {age} years old today.{n}" | |
F.Append(msg) = "He's been an adult for {age - 18} years now." | |
F.Append(msg) = "{n}{q}Wow that's old!{q}, said Kate." | |
msgbox msg, vbOkOnly, title | |
F.Tokens = ";" | |
msgbox F("Tokens can be changed.;n;So you can include {brackets} in your message.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment