Click here to Skip to main content
15,886,638 members
Articles / Programming Languages / VBScript

printf()-like Format Function in VBScript

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

Overview

The function fmt helps you to format a string in VBScript as you do in C.

In C, if you write:

VBScript
printf( "this is %s number %d", "test", 1 );

then you would use the function fmt in VBScript like this:

VBScript
dim str
str = fmt( "this is %x number %x", Array("test", 1) )

Details

The complete function looks like this:

VBScript
' 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.
		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

License

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


Written By
Chief Technology Officer Zeta Software GmbH
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.

➡️ Give me a tip 🙂

In his free time, he does climbing, running and mountain biking. In 2012 he became a father of a cute boy and in 2014 of an awesome girl.

Some cool, free software from us:

Windows 10 Ereignisanzeige  
German Developer Community  
Free Test Management Software - Intuitive, competitive, Test Plans.  
Homepage erstellen - Intuitive, very easy to use.  
Offline-Homepage-Baukasten

Comments and Discussions

 
GeneralPython like string interpolation Pin
Sandeep Datta30-Oct-09 9:22
Sandeep Datta30-Oct-09 9:22 
Generalenhancement to fmt function Pin
jasmit23330-Mar-07 8:23
jasmit23330-Mar-07 8:23 
GeneralRe: enhancement to fmt function Pin
Ludvik Jerabek14-Dec-07 18:06
Ludvik Jerabek14-Dec-07 18:06 
GeneralFormatStr Pin
Michael Cessna28-Jan-05 9:59
Michael Cessna28-Jan-05 9:59 
Questionalternate? Pin
clonel16-Nov-03 16:09
sussclonel16-Nov-03 16:09 
Generalresponse.write flag Pin
13-May-02 5:38
suss13-May-02 5:38 
GeneralGreat Idea Pin
Gratz3-Aug-00 12:02
Gratz3-Aug-00 12:02 
GeneralRe: Great Idea Pin
Uwe Keim3-Aug-00 18:51
sitebuilderUwe Keim3-Aug-00 18: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.