Click here to Skip to main content
Click here to Skip to main content

printf()-like Format Function in VBScript

By , 20 Jan 2000
Rate this:
Please Sign up or sign in to vote.

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:

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

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:
 
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
Some random fun stuff in German

Comments and Discussions

 
GeneralPython like string interpolation PinmemberSDX200030-Oct-09 9:22 
Generalenhancement to fmt function Pinmemberjasmit23330-Mar-07 8:23 
GeneralRe: enhancement to fmt function PinmemberLudvik Jerabek14-Dec-07 18:06 
GeneralFormatStr PinmemberMichael Cessna28-Jan-05 9:59 
Questionalternate? Pinsussclonel16-Nov-03 16:09 
Generalresponse.write flag PinmemberAnonymous13-May-02 5:38 
GeneralGreat Idea PinsussGratz3-Aug-00 12:02 
GeneralRe: Great Idea PinsussUwe Keim3-Aug-00 18:51 

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

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

| Advertise | Privacy | Mobile
Web02 | 2.8.140415.2 | Last Updated 21 Jan 2000
Article Copyright 2000 by Uwe Keim
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid