JavaScript equivalent to printf/String.Format

前端 未结 30 2412
囚心锁ツ
囚心锁ツ 2020-11-21 04:27

I\'m looking for a good JavaScript equivalent of the C/PHP printf() or for C#/Java programmers, String.Format() (IFormatProvider for .

30条回答
  •  别跟我提以往
    2020-11-21 05:10

    From ES6 on you could use template strings:

    let soMany = 10;
    console.log(`This is ${soMany} times easier!`);
    // "This is 10 times easier!
    

    Be aware that template strings are surrounded by backticks ` instead of (single) quotes.

    For further information:

    https://developers.google.com/web/updates/2015/01/ES6-Template-Strings

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/template_strings

    Note: Check the mozilla-site to find a list of supported browsers.

提交回复
热议问题