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

String.Format equivalent for ASP

Rate me:
Please Sign up or sign in to vote.
4.20/5 (11 votes)
2 Dec 2008CPOL 51.4K   17   13
VBScript/ASP version of C# String.format("this {0} a {1}","is","test").

Introduction

This is a String.Format equivalent function for classic ASP.

Background

I am working on an old ASP classic application (fixing bugs etc.). Have to say, I still love traditional classic ASP.

It takes moments to do something that would normally take hours in .NET; plus, it always feels so much faster to execute =)

Anyway, I needed a function similar to String.Format in C#. I could not find anything on Google, so I created my own and thought I would share.

Using the code

The code speaks for itself I think!

VBScript
Function StringFormat(sVal, aArgs)
Dim i
    For i=0 To UBound(aArgs)
        sVal = Replace(sVal,"{" & CStr(i) & "}",aArgs(i))
    Next
    StringFormat = sVal
End Function

StringFormat("this {0} a tes{1} string containing {2} values", _
             Array("is","t","some"))

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 MammothWorkwear.com
United Kingdom United Kingdom
Senior Web Developer, Systems Architect and Entrepreneur. Technical Director of MammothWorkwear.com. More information is available at http://www.johndoherty.info/

Comments and Discussions

 
GeneralGood insight Pin
Donsw13-Dec-08 7:08
Donsw13-Dec-08 7:08 
GeneralOther Methodologies to apply are always welcome... Pin
Tweellt10-Dec-08 4:27
Tweellt10-Dec-08 4:27 
GeneralThanks for the snippet Pin
stylesie8-Dec-08 15:24
stylesie8-Dec-08 15:24 
GeneralRe: Thanks for the snippet Pin
bjahelka18-Dec-08 14:12
bjahelka18-Dec-08 14:12 
GeneralIt takes moments to do something that would normally take hours in .Net, plus it always feels so much faster to execute =) Pin
brianma8-Dec-08 12:50
professionalbrianma8-Dec-08 12:50 
GeneralRe: It takes moments to do something that would normally take hours in .Net, plus it always feels so much faster to execute =) Pin
stylesie8-Dec-08 15:17
stylesie8-Dec-08 15:17 
GeneralThat can be done in just one line using RegEx Pin
Federico Colombo5-Dec-08 14:02
Federico Colombo5-Dec-08 14:02 
GeneralRe: That can be done in just one line using RegEx Pin
AndrewR738-Dec-08 6:25
AndrewR738-Dec-08 6:25 
GeneralSimilar to my printf Article Pin
Uwe Keim2-Dec-08 10:18
sitebuilderUwe Keim2-Dec-08 10:18 
GeneralRe: Similar to my printf Article Pin
John Doherty2-Dec-08 10:30
John Doherty2-Dec-08 10:30 
GeneralRe: Similar to my printf Article Pin
John Doherty2-Dec-08 10:36
John Doherty2-Dec-08 10:36 
GeneralMy vote of 2 Pin
rickoshay2-Dec-08 8:54
rickoshay2-Dec-08 8:54 
GeneralRe: My vote of 2 Pin
Gevorg3-Dec-08 4:59
Gevorg3-Dec-08 4:59 
Dude? Are you dumb?
There are only 5 lines of code. Do you need an explanations for that?

My site - Body Jewelry
Not my site - Piercing info

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.