stringformat
stringformat extends the String class to add python like formatting.
instance methods of String
- format(formatValue1, formatValue2, ... )
Formats a string replacing formatting specifiers with values provided as arguments which are formatted according to the specifier.
This is an implementation of python's % operator for strings and is similar to sprintf from C.
Usage:
resultString = formatString.format(value1, v2, ...);
Each formatString can contain any number of formatting specifiers which are
replaced with the formated values.
specifier([...]-items are optional):
"%(key)[flag][sign][min][percision]typeOfValue"
(key) If specified the 1st argument is treated as an objec/associative array and the formating values
are retrieved from that object using the key.
flag:
0 Use 0s for padding.
- Left justify result, padding it with spaces.
Use spaces for padding.
sign:
+ Numeric values will contain a +|- infront of the number.
min:
l The string will be padded with the padding character until it has a minimum length of l.
percision:
.x Where x is the percision for floating point numbers and the lenght for 0 padding for integers.
typeOfValue:
d Signed integer decimal.
i Signed integer decimal.
b Unsigned binary. //This does not exist in python!
o Unsigned octal.
u Unsigned decimal.
x Unsigned hexidecimal (lowercase).
X Unsigned hexidecimal (uppercase).
e Floating point exponential format (lowercase).
E Floating point exponential format (uppercase).
f Floating point decimal format.
F Floating point decimal format.
c Single character (accepts byte or single character string).
s String (converts any object using object.toString()).
Examples:
"%02d".format(8) -> "08"
"%05.2f".format(1.234) -> "01.23"
"123 in binary is: %08b".format(123) -> "123 in binary is: 01111011"
parameters:
- ...
Each parameter is a formating value which will be inserted into the string.
If key mapping is used then only one value is expected which is either a associative array or an object.
return value:
A new formated string.