sprintf的相当于客户端JavaScript [英] sprintf equivalent for client-side JavaScript

查看:119
本文介绍了sprintf的相当于客户端JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道的console.log 通过乱搞至少支持一些的printf 的从C的基本特征,但我很好奇的方法来利用的console.log 中的实现创建一个类似于的sprintf 的东西。我知道你不能简单地使用 .bi​​nd 。适用,因为的console.log 实际上并不返回字符串,那么有没有办法解决?

如果这不是实际上可能是有一些其他鲜为人知的原生实现从JavaScript中实现的sprintf 只有code几行了吗?

对于那些谁不知道什么的sprintf 正好,的这里是tutorialspoint 的一些文档。用法示例我正在寻找的是如下:

  VAR字符串1 = sprintf的(你好,%S!,世界);
VAR字符串2 = sprintf的(一切问题的答案为%d,42);


解决方案

尝试利用<击> 评估 .replace

\r
\r

VAR的sprintf =功能的sprintf(){\r
  //参数\r
  变参= Array.prototype.slice.call(参数)\r
    字符串参数//\r
  中,n = args.slice(1,-1)\r
    //字符串\r
  ,文字= ARGS [0]\r
    //检查`Number`\r
  ,_res = isNaN(parseInt函数(参数[args.length - 1]))\r
             ? ARGS [args.length - 1]\r
               //或者,如果字符串传递\r
               //作为最后一个参数`sprintf`,\r
               //'的eval(参数[args.length - 1])`\r
             :号码(参数[args.length - 1])\r
    //替换值数组\r
  ,编曲= n.concat(_res)\r
    //`res`:`text`\r
  ,RES =文本;\r
  //循环`arr`项目\r
  对于(VAR I = 0; I&LT; arr.length;我++){\r
    //内`res`用`arr`在索引`i`更换格式化字符\r
    RES = res.replace(/%d个|%S /,编曲[I])\r
  }\r
  //返回字符串`res`\r
  返回水库\r
};\r
\r
的document.write(sprintf的(%d个加%d为%D,0,1,0 + 1)\r
               +&LT; BR&gt;中\r
               + sprintf的(你好,%S!,世界)\r
               +&LT; BR&gt;中\r
               + sprintf的(一切问题的答案为%d,42)\r
              );

\r

\r
\r

I know that console.log supports at least some of the basic features of printf from C through messing around, but I was curious of a way to take advantage of console.log's implementation to create something similar to sprintf. I know you can't simply use .bind or .apply since console.log doesn't actually return the string, so is there a way around this?

If this isn't actually possible, is there some other little-known native implementation that's only a few lines of code away from achieving sprintf in JavaScript?

For those who do not know what sprintf is exactly, here is some documentation from tutorialspoint. Example usage I'm looking for is below:

var string1 = sprintf("Hello, %s!", "world");
var string2 = sprintf("The answer to everything is %d.", 42);

解决方案

Try utilizing eval , .replace

var sprintf = function sprintf() {
  // arguments
  var args = Array.prototype.slice.call(arguments)
    // parameters for string
  , n = args.slice(1, -1)
    // string
  , text = args[0]
    // check for `Number`
  , _res = isNaN(parseInt(args[args.length - 1])) 
             ? args[args.length - 1] 
               // alternatively, if string passed
               // as last argument to `sprintf`,
               // `eval(args[args.length - 1])`
             : Number(args[args.length - 1]) 
    // array of replacement values
  , arr = n.concat(_res)
    // `res`: `text`
  , res = text;
  // loop `arr` items
  for (var i = 0; i < arr.length; i++) {
    // replace formatted characters within `res` with `arr` at index `i`
    res = res.replace(/%d|%s/, arr[i])
  }
  // return string `res`
  return res
};

document.write(sprintf("%d plus %d is %d", 0, 1, 0 + 1) 
               + "<br>" 
               + sprintf("Hello, %s!", "world") 
               + "<br>" 
               + sprintf("The answer to everything is %d.", 42)
              );

这篇关于sprintf的相当于客户端JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆