sprintf for VBScript?

Discussion in 'ASP General' started by Justin, Aug 11, 2004.

  1. Justin

    Justin Guest

    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
    Justin, Aug 11, 2004
    #1
    1. Advertising

  2. 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)
    %>


    --
    http://www.aspfaq.com/
    (Reverse address to reply.)




    "Justin" <ng@NO_SPAMmaritimeNO_SPAMsource.ca> wrote in message
    news:...
    > 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
    Aaron [SQL Server MVP], Aug 11, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. shea martin

    sprintf

    shea martin, Sep 2, 2004, in forum: Java
    Replies:
    5
    Views:
    3,594
    shea martin
    Sep 3, 2004
  2. Pep
    Replies:
    5
    Views:
    4,067
  3. CJ
    Replies:
    1
    Views:
    1,325
    Davlet Panech
    Oct 28, 2003
  4. Mike Chirico
    Replies:
    2
    Views:
    3,844
    Grumble
    Nov 19, 2003
  5. Pilatus
    Replies:
    3
    Views:
    547
    Pilatus
    Dec 18, 2003
Loading...

Share This Page