Recently I've been forced to do a lot of ASP coding using VBScript, and immediately I
sorely missed the slot-based string formating functions available in most other languages.
In Python, for instance, you could do this:
result = "Hello %s! How are you this %s?" % (name, "morning")
Well, even more useful is to have named, or indexed, slots so you can do I18N
without code changes, or reuse parameters without repeating them in the argument list.
The SDK function
FormatMessage does this for you if you are
programming in a "real" language.
I decided to whip up a very simple
for my own use. I'm sure most everybody coding in VBScript has done something similar.
In case you haven't done so already, feel free to use this code. Enjoy!
Well, here it is in all it's glory.
function Fmt(str, args)
res = str
for i = 0 to UBound(args)
res = Replace(res, "%"&CStr(i+1)&"%", args(i))
res = Replace(res, "\n", vbCrLf)
res = Replace(res, "\t", vbTab)
fmt = res
You call it like so:
str = Fmt("<%1%>This is a %2%</%1%>", Array("div", "test"))
That's all there is to it. You use slot markers numbered from 1 placed between %-signs, e.g.
%1%. If you use more slots than you have arguments the extraneuos ones will
simply be left in the string. The arguments are passed in an
Array. Since VBScript
only uses VARIANT-types, you can put anything in here and the script engine will do it's best to
convert it to a string.
I also added the convenience of using
Tab and NewLine respectively.
This code is somewhat similar to an
article posted by Uwe Keim
a long time ago. My approach is considerably simpler and, I think, more efficient.
If you have any comments on this article, you can contact me at