What's new? | Help | Directory | Sign in
Google
                
Search
for
Updated May 17, 2008 by hhh333
Labels: Featured
StringFormat  

jQuery-utils: strings

Introduction

Format is an implementation of the Python 3k Advanced String Formatting.

Usage examples

Simple replacement

// all return "1bc"
$.format('{a}bc', {a:'1'}) // named arguments
$.format('{0}bc', [1])     // array arguments
$.format('{0}bc', 1)       // normal arguments

Type conversion

$.format('{a:d}bc', {a:'a1'})  // return "1bc"

$.format('{a:d}bc', {a:1.5})   // return "1bc"

$.format('{a:.2f}bc', {a:'1'}) // returns 1.00bc

Padding

$.format('{a:08.2f}bc', {a:'1'}) // return 00001.00bc

User defined formatting

$.extend(jQuery.strConversion, 
    {'U': function(input, arg){ return input.toUpperCase(); }
});

$.format('{0:U}bc', 'a') // return Abc

Known differences

The conversion flags

Flag Meaning
# The value conversion will use the ``alternate form'' (where defined below).
0 The conversion will be zero padded for numeric values.
- The converted value is left adjusted (overrides the "0" conversion if both are given). (1)
(a space) A blank should be left before a positive number (or empty string) produced by a signed conversion. (1)
+ A sign character ("+" or "-") will precede the conversion (overrides a "space" flag). (1)

Note:

  1. Not supported yet

Conversions types

Conversion Meaning Notes
d Signed integer decimal.
i Signed integer decimal.
o Unsigned octal. (1)
u Unsigned decimal.
x Unsigned hexadecimal (lowercase). (2)
X Unsigned hexadecimal (uppercase). (2)
e Floating point exponential format (lowercase). (3)
E Floating point exponential format (uppercase). (3)
f Floating point decimal format. (3)
F Floating point decimal format. (3)
g Floating point format. Uses exponential format if exponent is greater than -4 or less than precision, decimal format otherwise. (4)
G Floating point format. Uses exponential format if exponent is greater than -4 or less than precision, decimal format otherwise. (4)
c Single character (accepts integer or single character string).
r String (converts any JavaScript object using repr()). (5)
s String (converts any JavaScript object using toString()). (6)

Notes:

  1. The alternate form causes a leading zero ("0") to be inserted between left-hand padding and the formatting of the number if the leading character of the result is not already a zero.
  2. The alternate form causes a leading '0x' or '0X' (depending on whether the "x" or "X" format was used) to be inserted between left-hand padding and the formatting of the number if the leading character of the result is not already a zero.
  3. The alternate form causes the result to always contain a decimal point, even if no digits follow it. The precision determines the number of digits after the decimal point and defaults to 6.
  4. The alternate form causes the result to always contain a decimal point, and trailing zeroes are not removed as they would otherwise be. The precision determines the number of significant digits before and after the decimal point and defaults to 6.
  5. The %r conversion was added in Python 2.0. The precision determines the maximal number of characters used.
  6. If the object or format provided is a unicode string, the resulting string will also be unicode. The precision determines the maximal number of characters used.

Source: http://docs.python.org/lib/typesseq-strings.html

Tested browsers

Note: if you pass/fail the test suite on other browser please send me your browser string with the results, thanks.

TODO

References


Sign in to add a comment