7

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,

0

1 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

2 Comments

@PeterSzymkowski - True, which is why I said that JavaScript does not support this natively. I'll add that link to my answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.