Loading ...
Sorry, an error occurred while loading the content.

Re: [ydn-javascript] String substitution

Expand Messages
  • Gopalarathnam Venkatesan
    ... Hi Peter: AFAIK there is none, but its not difficult to write one. Here s one way to write it: function myprintf() { var i, fmt = arguments[0], n =
    Message 1 of 3 , Nov 1, 2006
    • 0 Attachment
      foti-1@... wrote:
      >
      >
      > I'm looking for something that will let me add placeholders to a string,
      > and then substitute those with values. For example, the string might
      > look like this:
      >
      > "Hello {0}. There were {1} results. You can delete {1} objects."
      >
      > And {0} would be replaced by the first argument, {1} by the second
      > argument, and so on. Something like:
      >
      > substitute("Hello {0}. There were {1} results. You can delete {1}
      > objects.", "Peter", 5);
      >
      > Which would produce:
      >
      > Hello Peter. There were 5 results. You can delete 5 objects.
      >
      > Anyone know of anything that does something like this?

      Hi Peter:

      AFAIK there is none, but its not difficult to write one. Here's one way
      to write it:

      function myprintf()
      {
      var i, fmt = arguments[0], n = arguments.length, s;

      for (i = 1, s = fmt; i < n; i++) {
      s = s.replace("{"+(i-1)+"}", arguments[i], "g");
      }

      return s;
      }

      myprintf("Hello {0}. There were {1} results. You can delete {1}
      objects.", "Peter", 5);
      --
      Gopalarathnam Venkatesan

      http://gopalarathnam.com/
    • Peter Foti
      Thanks. I m surprised this sort of thing is not more common. Peter ... From: Gopalarathnam Venkatesan To: foti-1@comcast.net Cc:
      Message 2 of 3 , Nov 1, 2006
      • 0 Attachment
        Thanks.  I'm surprised this sort of thing is not more common.
         
        Peter
         
        ----- Original Message -----
        Sent: Wednesday, November 01, 2006 11:43 PM
        Subject: Re: [ydn-javascript] String substitution

        foti-1@comcast. net wrote:
        >
        >
        > I'm looking for something that will let me add placeholders to a string,
        > and then substitute those with values. For example, the string might
        > look like this:
        >
        > "Hello {0}. There were {1} results. You can delete {1} objects."
        >
        > And {0} would be replaced by the first argument, {1} by the second
        > argument, and so on. Something like:
        >
        > substitute(" Hello {0}. There were {1} results. You can delete {1}
        > objects.", "Peter", 5);
        >
        > Which would produce:
        >
        > Hello Peter. There were 5 results. You can delete 5 objects.
        >
        > Anyone know of anything that does something like this?

        Hi Peter:

        AFAIK there is none, but its not difficult to write one. Here's one way
        to write it:

        function myprintf()
        {
        var i, fmt = arguments[0] , n = arguments.length, s;

        for (i = 1, s = fmt; i < n; i++) {
        s = s.replace("{ "+(i-1)+" }", arguments[i] , "g");
        }

        return s;
        }

        myprintf("Hello {0}. There were {1} results. You can delete {1}
        objects.", "Peter", 5);
        --
        Gopalarathnam Venkatesan

        http://gopalarathna m.com/

      Your message has been successfully submitted and would be delivered to recipients shortly.