2012-07-18 43 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