Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

A full implementation of Python string formatting in JavaScript.

branch: master

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 MIT-LICENSE.txt Initial import March 07, 2010
Octocat-spinner-32 README.md Correct file extension for the README March 07, 2010
Octocat-spinner-32 stringformat.js Fixing string lookups for full MSIE support. April 03, 2010
Octocat-spinner-32 test.html Fixing string lookups for full MSIE support. April 03, 2010
README.md

Stringformat

Provides a full implementation of Python style string formatting for JavaScript as outlined in the Python documentation: http://docs.python.org/library/stdtypes.html#string-formatting-operations

The function comes attached to String, and you may use it like that:

String.format( "I have %d apples, and %d pears.", [ 13, 10 ] );

It supports a simple argument:

String.format( "I have %d apples.", 9 );

If you want to move the function to string prototype where, arguably, it may be more useful:

String.prototype.format = function () {
  return String.format( this, arguments );
};

"I have %d apples, and %d pears..".format( 9, 12 );

Additionally, the function support a noConflict method which allows you to unassign it from String and into whatever name you want.

$.format = String.format.noConflict();
Something went wrong with that request. Please try again.