5

Here is something I used in NSString ...

[NSString stringWithFormat:@"This is a digit %d", 10];

the 10 value will go to %d...., and the string will become "This is a digit 10", is there any similar thing in javascript? Thank you... Also, I would like to know, what is this call??

MusiGenesis
  • 74,184
  • 40
  • 190
  • 334
Tattat
  • 15,548
  • 33
  • 87
  • 138
  • 2
    it's called [sprintf](http://www.diveintojavascript.com/projects/javascript-sprintf) – Khez Apr 15 '11 at 15:38
  • 1
    possible duplicate of [Javascript printf/string.format](http://stackoverflow.com/questions/610406/javascript-printf-string-format) – codymanix Apr 15 '11 at 15:41

8 Answers8

4

There is no built-in string formatting, but you can use a JavaScript library to do the same: sprintf().

Matt Ball
  • 354,903
  • 100
  • 647
  • 710
4

You can concatenate strings easily in Javascript:

  var str = "This is a digit " + 10;
dcai
  • 2,557
  • 3
  • 20
  • 37
2

To achieve the same effect, you can just say:

"This is a digit " + 10;

Alternatively, if you need actual string formatting you may want to have a look at javascript-printf-string-format.

Community
  • 1
  • 1
Justin Ethier
  • 131,333
  • 52
  • 229
  • 284
1

There is no such thing in javascript but you can build your own printf.

codymanix
  • 28,510
  • 21
  • 92
  • 151
1

This is possible using regular expressions.

http://www.regular-expressions.info/javascript.html This is a fairly good website for the topic, refer to the section 'Replacement Text Syntax'.

GAgnew
  • 3,847
  • 3
  • 26
  • 28
0

bob.js JS framework allows doing this:

var sFormat = "My name is {0} and I am version {1}.0.";
var result = bob.string.formatString(sFormat, "Bob", 1);
console.log(result);
//output:
//==========
// My name is Bob and I am version 1.0.

- Tengiz

Tengiz
  • 8,011
  • 30
  • 39
0

Here's a simple sprintf implementation I found. It's built as an angular filter but the logic can of course be pulled out.

https://gist.github.com/jakobloekke/7303217

angular.module('filters')
     .filter('sprintf', function() {

    function parse(str) {
        var args = [].slice.call(arguments, 1),
            i = 0;

        return str.replace(/%d/g, function() {
            return args[i++];
        });
    }

    return function() {
        return parse(
            Array.prototype.slice.call(arguments, 0,1)[0], 
            Array.prototype.slice.call(arguments, 1)
        );
    };
});

Usage:

$filter('sprintf')(
    "Hello %d. It's %d nice to see you!",
    "World",
    "very"
);

or

scope.values = ["World", "very"];

<p ng-bind="message | sprintf: values"></p>
parliament
  • 21,544
  • 38
  • 148
  • 238
0

Nowadays, we have template strings, which accomplish a very similar thing. They work like this:

var message = "Hello world"
console.log(`This is my message: ${message}. Don't you love it?`)

Template literals are identified by the `` defining them and using ${var} to include variables.

johnpyp
  • 867
  • 2
  • 7
  • 13
  • I don't see this as an improvement. It's not really any different to `console.log ("This is my message: " + message + ". Don't you love it?")`. The whole point of formatting (as implemented by the Delphi routine `Format (...` that I'm familiar with) is that your format strings become a whole lot more readable. `Log ('My name is %s %s and I am %d years old', [FirstName, SecondName, Age])` is more eye-friendly than `Log ('My name is ' + FirstName + ' ' + SecondName + ' and I am ' + IntToStr (Age) + ' years old')`. – rossmcm Jul 29 '21 at 22:12