JavaScript equivalent to printf/string.format

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

My basic requirement is a thousand separator format for numbers for now, but something that handles lots of combinations (including dates) would be good.

I realize Microsoft's Ajax library provides a version of String.Format(), but we don't want the entire overhead of that framework.

Text Copied From stackoverflow.com
on : Wed, Mar 04, 2009 Views : 1215789

Accepted Answer

Try sprintf() for JavaScript.


Update    Ok, if you really want to do a simple format method on your own, don’t do the replacements successively but do them simultaneously.

Because most of the other proposals that are mentioned fail when a replace string of previous replacement does also contain a format sequence like this:



{
0
}
{
1
}.format(
{
1
}, 
{
0
}) 

Normally you would expect the output to be

{
1
}
{
0
}
but the actual output is

{
1
}
{
1
}
. So do a simultaneously replacement instead like in fearphage’s suggestion.

Text Copied From stackoverflow.com
on : Wed, Mar 04, 2009

Ask Question

If you want to ask question you must LogIn or SignUp
Login SignUp

Quick Links

E-Magazines

@

Total Followers
Study Group Created
Study Group Joined
Following Teacher
Following Organization
Blog Articles Added
Questions Asked
Questions Answered
Jobs Posted
Total Members in Group
Questions asked by members
Tasks added in this Group

Please wait..

Ok

Login to Open ESchool OR Create your account    Login   SignUp