Overview
The function fmt
helps you to format a string in VBScript as you do in C.
In C, if you write:
printf( "this is %s number %d", "test", 1 );
then you would use the function fmt
in VBScript like this:
dim str
str = fmt( "this is %x number %x", Array("test", 1) )
Details
The complete function looks like this:
function fmt( str, args )
dim res res = ""
dim pos pos = 0
dim i
for i = 1 to Len(str)
if Mid(str,i,1)="%" then
if i<Len(str) then
if Mid(str,i+1,1)="%" then
res = res & "%"
i = i + 1
elseif Mid(str,i+1,1)="x" then
res = res & CStr(args(pos))
pos = pos+1
i = i + 1
end if
end if
else
res = res & Mid(str,i,1)
end if
next
fmt = res
end function
The format character is always %x
, independent of the actual type, since VBScript has no direct types like integer or string.
Improve Me!
The function fits my needs where I used it, but can be extended in some ways to behave more like printf
:
- Format characters can be extended, i.e., the
%x
could be divided up into %d
for integers, %x
for hex numbers, %f
for float, etc.
- The other
printf
features like leading zeros and all that stuff could be added too.
Epilog
As always: my tip for editing VBScript files: Tried a lot of editors (including Frontpage, InterDev, etc.), I found the most usable program is EditPlus, which you can find on www.editplus.com (no, I'm not getting money from them).
Please feel free to ask any questions you have by e-mail: keim@zeta-software.de.
History
- 20th January, 2000: Initial post