JavaScript String Format: Format text string according to a pattern

Recommend this page to a friend!
  Info   View files Example   View files View files (3)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
StarStarStarStar 68%Total: 111 All time: 398 This week: 17Up
Version License JavaScript version Categories
stringformat 1.0GNU General Publi...5Text processing, Templates
Description Author

This object can format text string according to a pattern.

It adds a prototype to an object that can format a text string expanding certain marks in a given format.

The format marks can determine if the input parameters should be taken as text or numbers. Numbers can be formatted as decimal, hexadecimal or octal.

The formatted values can be justified with spaces and aligned to the left or right.

Innovation Award
JavaScript Programming Innovation award nominee
May 2016
Number 2
Most languages provide standard libraries that allow string manipulation operations like formatting a text string with a list of parameters values. However, JavaScript does not come with any built-in objects for that purpose.

This package can add a prototype to an object to let it format strings in a similar way to sprintf functions of other functions.

Manuel Lemos
Picture of Sergio Flores Genis
  Performance   Level  
Name: Sergio Flores Genis <contact>
Classes: 1 package by
Country: Mexico Mexico
Age: 31
All time rank: 1586 in Mexico Mexico
Week rank: 6 Up1 in Mexico Mexico Up
Innovation award
Innovation award
Nominee: 1x



Each function call requires a format string and a list of arguments. The format string can be a fixed text string or one or more inlays that specify the format. Format specifiers begin and end in braces {...}. The general format for formatting strings or numbers is:

  • argument_index: (required) is the unsigned integer indicating the index of the argument to take from the list. The index is numbered from zero to n (0, 1, 2, 3 ... n).
  • align: (optional) character - (minus) indicates that the text is aligned to the left.
  • char: (optional) a character with the apostrophe prefix, fill in the result when the width indicated.
  • width: (optional) is the unsigned integer indicating the filling result.
  • conversion: (required if) Conversion argument to be formatted. "s" used to string and default if not indicated. "n" used to format numbers.
  • arguments: see below.
  • object_property: if the argument is an object that allows him to read a property. The point separates sub properties.

Arguments for numbers (n)

The following table shows the supported arguments.

| Flag | Description | |------|-------------------------------------------------------------| | + | The result will always include a sign | | # | The result will include a leading space for positive values | | , | The result will include locale-specific grouping separators |

  Files folder image Files  
File Role Description
Accessible without login Plain text file index.html Example Example for use
Accessible without login Plain text file README.txt Doc. Syntax Explanation
Plain text file String.strformat.js Class The source code

 Version Control Unique User Downloads Download Rankings  
This week:0
All time:398
This week:17Up
 User Ratings  
 All time