sprintf for VBScript?

J

Justin

Hi,

I'm looking for the asp equivalent of PHP's sprintf/printf
function...In PHP you can do this:

$format = "There are %d monkeys in the %s";
printf($format,$num,$location);

This might output, "There are 5 monkeys in the tree".

You can also specify the order of the placeholders and repeat the
placeholders without adding more arguments in the code. Argument
swapping:

$format = "The %2\$s contains %1\$d monkeys. That's a nice %2\$s full
of %1\$d monkeys.";
printf($format, $num, $location);

Output, "The tree contains 5 monkeys. That's a nice tree full of 5
monkeys."

Any suggestions to acoomplish something similar with VBScript?

Thanks
 
A

Aaron [SQL Server MVP]

Yes, write your own function that replaces tokens (I don't believe VBScript
has anything built in that will magically do this for you).

<%
function printf(str, t1, t2)
printf = replace(str, "%t1", t1)
printf = replace(printf, "%t2", t2)
end function

format = "The %t2 contains %t1 monkeys. " & _
"That's a nice %t2 full of %t1 monkeys."
num = 5
location = "tree"
response.write printf(format, num, location)
%>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top