2012-07-18 45 views
0

可能重复:
JavaScript equivalent to printf/string.format打印在javascript复杂的字符串,而不+符号

有没有像这样一个以上的C打印复杂的字符串在JavaScript中,最好是不需要大量的+符号和伴随的""

我想这样:

console.log ("Name: %s Age: %s Sex %s Wieght %s Height %s", name, age, sex, weight, height); 

取而代之的是:

console.log ("Name: " + name + " Age: " + age + " Sex: " + sex + " Weight: " + weight " Height: " + height); 
+0

http://stackoverflow.com/questions/610406/javascript-equivalent-to-printf-string-format – Lance 2012-07-18 21:15:39

+0

@兰斯我的坏。我搜索了除“javascript ... printf”之外的所有内容。我会投票结束 – puk 2012-07-18 21:17:44

回答

1

你说的是标准window.console?它有完全相同的功能(在Firefox测试):

console.log("Name: %s Age: %s Sex %s Wieght %s Height %s", name, age, sex, weight, height); 

或者:

console.log("Name: ", name, " Age: ", sex, " Sex ", sex, " Wieght ", weight", " Height ", height); 
+1

现在我觉得只是简单的愚蠢 – puk 2012-07-18 21:29:37

2

这个库似乎做你想要什么,如果你不介意的第三方。

JavaScript sprintf

从使用CONSOLE.LOG你总是可以做到这一点,而不是拼接的文章

vsprintf('The first 4 letters of the english alphabet are: %s, %s, %s and %s', ['a', 'b', 'c', 'd']); 
0

console.log ("Name: ", name, " Age: ", age, " Sex: ", sex, " Weight: ", weight, " Height: " , height); 
+0

这还不够美观 – puk 2012-07-18 21:18:07