Implementation of PHP sprintf function instance _javascript technique based on JS

Source: Internet
Author: User
Tags sprintf

This article describes the implementation of PHP based on JS sprintf function. Share to everyone for your reference, specific as follows:

The functions are as follows:

<script type= "Text/javascript" >
function sprintf ()
{
  var arg = arguments,
    str = arg[0] | | ",
    I, N;
  for (i = 1, n = arg.length i < n; i++) {
    str = str.replace (/%s/, arg[i]);
  return str;
}
</script>

The first parameter is a string containing the "%s", and the other parameters are the corresponding variables to replace "%s".

Such as:

<script type= "Text/javascript" >
var str = "Bed before%s bright, suspected to be frost on%s; ",
  var1 =" Ming ",
  var2 =" ground ",
  var3 =" Hope ",
  VAR4 =" think ";
str = sprintf (str, var1, VAR2, Var3, VAR4);
</script>

I hope this article will help you with JavaScript programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.