String.Format() In JavaScript As In C#

This is a cool script that gives the same effect as the C# string formating with multiple parameters. Check it out:

function format(str)

{

  for(i = 1; i < arguments.length; i++)

  {

    str = str.replace(‘{‘ + (i – 1) + ‘}’, arguments[i]);

  }

  return str;

}

Example: <font color=”#008000″>greeting = format(‘Hello {0} & {1} ‘, ‘John’, ‘Jane’);</font>

Advertisements