Possible Duplicate:
JavaScript equivalent to printf/string.format
I'm not sure the exact term (string substitution?) but many languages (C, VB, C#, etc.) offer similar mechanisms for constructing string dynamically. The following is an example in C#:
string firstName = "John";
string lastName = "Doe";
string sFinal = string.Format(" Hello {0} {1} !", firstName, lastName);
I'd would like to accomplish the same thing in JavaScript. Can anyone shed some light?
Thanks,
JavaScript does not yet have this functionality natively. You'll have to use concatenation:
var firstName = "John";
var lastName = "Doe";
var sFinal = " Hello " + firstName + " " + lastName + " !";
That sucks? True. But this is the world we live in.
As pointed out by @PeterSzymkowski, you can use this JavaScript implementation of the C/PHP sprintf
function.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With