我知道在PHP中我们可以这样做:
$hello = "foo";
$my_string = "I pity the $hello";
输出:"I pity the foo"
我想知道同样的事情在JavaScript中是否也是可能的。在字符串中使用变量而不使用连接-它看起来更简洁和优雅。
我知道在PHP中我们可以这样做:
$hello = "foo";
$my_string = "I pity the $hello";
输出:"I pity the foo"
我想知道同样的事情在JavaScript中是否也是可能的。在字符串中使用变量而不使用连接-它看起来更简洁和优雅。
当前回答
2020年和平语录:
Console.WriteLine("I {0} JavaScript!", ">:D<");
console.log(`I ${'>:D<'} C#`)
其他回答
在Firefox 34 / Chrome 41 / Safari 9 / Microsoft Edge之前,没有。尽管你可以尝试JavaScript的sprintf来实现:
var hello = "foo";
var my_string = sprintf("I pity the %s", hello);
2020年和平语录:
Console.WriteLine("I {0} JavaScript!", ">:D<");
console.log(`I ${'>:D<'} C#`)
在Firefox 34 / Chrome 41 / Safari 9 / Microsoft Edge之前,不,这在javascript中是不可能的。你将不得不求助于:
var hello = "foo";
var my_string = "I pity the " + hello;
Var hello = "foo";
var my_string ="I pity the";
console.log (my_string,你好)
简单的使用方法:
var util = require('util');
var value = 15;
var s = util.format("The variable value is: %s", value)