Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript Composite String [duplicate]

Tags:

javascript

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,

like image 791
archenil Avatar asked Jan 28 '13 02:01

archenil


1 Answers

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.

like image 73
Joseph Silber Avatar answered Nov 01 '22 20:11

Joseph Silber