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

FormatMessage-like formatting in VBScript

Rate me:
Please Sign up or sign in to vote.
2.50/5 (2 votes)
11 Feb 2002Public Domain1 min read 60.3K   18   3
A simple function to do FormatMessage-like formatting in VBScript

Overview

Recently I've been forced to do a lot of ASP coding using VBScript, and immediately I sorely missed the slot-based string formating functions available in most other languages.

In Python, for instance, you could do this:

result = "Hello %s! How are you this %s?" % (name, "morning")

Well, even more useful is to have named, or indexed, slots so you can do I18N without code changes, or reuse parameters without repeating them in the argument list. The SDK function FormatMessage does this for you if you are programming in a "real" language.

I decided to whip up a very simple FormatMessage look-alike for my own use. I'm sure most everybody coding in VBScript has done something similar. In case you haven't done so already, feel free to use this code. Enjoy!

Details

Well, here it is in all it's glory.

VBScript
function Fmt(str, args)
    dim res
    res = str

    dim i
    for i = 0 to UBound(args)
        res = Replace(res, "%"&CStr(i+1)&"%", args(i))
    next

    res = Replace(res, "\n", vbCrLf)
    res = Replace(res, "\t", vbTab)

    fmt = res
end function

You call it like so:

VBScript
str = Fmt("<%1%>This is a %2%</%1%>", Array("div", "test"))

That's all there is to it. You use slot markers numbered from 1 placed between %-signs, e.g. %1%. If you use more slots than you have arguments the extraneuos ones will simply be left in the string. The arguments are passed in an Array. Since VBScript only uses VARIANT-types, you can put anything in here and the script engine will do it's best to convert it to a string.

I also added the convenience of using \t and \n for Tab and NewLine respectively.

Postscript

This code is somewhat similar to an article posted by Uwe Keim a long time ago. My approach is considerably simpler and, I think, more efficient.

If you have any comments on this article, you can contact me at svenax@bredband.net.

License

This article, along with any associated source code and files, is licensed under A Public Domain dedication


Written By
Web Developer
Sweden Sweden
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralOld wine in Old Bottle! Pin
Sandeep Datta4-Jan-07 3:59
Sandeep Datta4-Jan-07 3:59 
QuestionAdd validation in Faculty field Pin
viashu23-May-06 19:47
viashu23-May-06 19:47 
GeneralNice idea! Pin
Jörgen Sigvardsson12-Feb-02 9:36
Jörgen Sigvardsson12-Feb-02 9:36 

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.