Click here to Skip to main content
Licence CPOL
First Posted 20 Jan 2000
Views 94,946
Downloads 408
Bookmarked 17 times

printf()-like Format Function in VBScript

By | 20 Jan 2000 | Article
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:

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

Member

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 is also teached programming to students at the local university.
 
In his free time, he does climbing, running and mountain biking. You can watch him most of the day (and probably night) programming.
 
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


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralPython like string interpolation PinmemberSDX20009:22 30 Oct '09  
Generalenhancement to fmt function Pinmemberjasmit2338:23 30 Mar '07  
GeneralRe: enhancement to fmt function PinmemberLudvik Jerabek18:06 14 Dec '07  
GeneralFormatStr PinmemberMichael Cessna9:59 28 Jan '05  
Questionalternate? Pinsussclonel16:09 16 Nov '03  
Generalresponse.write flag PinmemberAnonymous5:38 13 May '02  
GeneralGreat Idea PinsussGratz12:02 3 Aug '00  
GeneralRe: Great Idea PinsussUwe Keim18:51 3 Aug '00  

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.

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