Click here to Skip to main content
15,167,925 members
Articles / Programming Languages / VBScript
Posted 20 Jan 2000

Tagged as


17 bookmarked

printf()-like Format Function in VBScript

Rate me:
Please Sign up or sign in to vote.
5.00/5 (7 votes)
20 Jan 2000CPOL
A format function in VBScript that simulates the printf() C function


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) )


The complete function looks like this:

' works like the printf-function in C.
' takes a string with format characters and an array
' to expand.
' the format characters are always "%x", independ of the
' type.
' usage example:
'	dim str
'	str = fmt( "hello, Mr. %x, today's date is %x.", Array("Miller",Date) )
'	response.Write str
function fmt( str, args )
	dim res		' the result string.
	res = ""

	dim pos		' the current position in the args array.
	pos = 0

	dim i
	for i = 1 to Len(str)
		' found a fmt char.
		if Mid(str,i,1)="%" then
			if i<Len(str) then
				' normal percent.
				if Mid(str,i+1,1)="%" then
					res = res & "%"
					i = i + 1

				' expand from array.
				elseif Mid(str,i+1,1)="x" then
					res = res & CStr(args(pos))
					pos = pos+1
					i = i + 1
				end if
			end if

		' found a normal char.
			res = res & Mid(str,i,1)
		end if

	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.


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 (no, I'm not getting money from them).

Please feel free to ask any questions you have by e-mail:


  • 20th January, 2000: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Uwe Keim
Chief Technology Officer Zeta Producer Desktop CMS
Germany Germany
Uwe does programming since 1989 with experiences in Assembler, C++, MFC and lots of web- and database stuff and now uses ASP.NET and C# extensively, too. He has also teached programming to students at the local university.

In his free time, he does climbing, running and mountain biking. Recently he became a father of a cute boy.

Some cool, free software from us:

Deutschsprachiges Forum zu Elektromobilität und Tesla  
Windows 10 Ereignisanzeige  
German Developer Community  
Free Test Management Software - Intuitive, competitive, Test Plans. Download now!  
Homepage erstellen - Intuitive, very easy to use. Download now!  
Send large Files online for free by Email

Comments and Discussions

GeneralPython like string interpolation Pin
Sandeep Datta30-Oct-09 10:22
MemberSandeep Datta30-Oct-09 10:22 
Generalenhancement to fmt function Pin
jasmit23330-Mar-07 9:23
Memberjasmit23330-Mar-07 9:23 
GeneralRe: enhancement to fmt function Pin
Ludvik Jerabek14-Dec-07 19:06
MemberLudvik Jerabek14-Dec-07 19:06 
GeneralFormatStr Pin
Michael Cessna28-Jan-05 10:59
MemberMichael Cessna28-Jan-05 10:59 
Questionalternate? Pin
clonel16-Nov-03 17:09
sussclonel16-Nov-03 17:09 
Generalresponse.write flag Pin
13-May-02 6:38
suss13-May-02 6:38 
GeneralGreat Idea Pin
Gratz3-Aug-00 13:02
MemberGratz3-Aug-00 13:02 
GeneralRe: Great Idea Pin
Uwe Keim3-Aug-00 19:51
sitebuilderUwe Keim3-Aug-00 19:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.