Extracted from Pike v7.4 release 35 at 2003-12-09.
pike.ida.liu.se
[Top]
predef::

Method sprintf()


Method sprintf

string sprintf(string format, mixed ... args)

Description

Print formated output to string.

The format string is a string containing a description of how to output the data in args . This string should generally speaking have one %<modifiers><operator> format specifier (examples: %s, %0d, %-=20s) for each of the arguments.

The following modifiers are supported:

'0'

Zero pad numbers (implies right justification).

'!'

Toggle truncation.

' '

Pad positive integers with a space.

'+'

Pad positive integers with a plus sign.

'-'

Left adjust within field size (default is right).

'|'

Centered within field size.

'='

Column mode if strings are greater than field size.

'/'

Rough line break (break at exactly field size instead of between words).

'#'

Table mode, print a list of '\n' separated word (top-to-bottom order).

'$'

Inverse table mode (left-to-right order).

'n'

(Where n is a number or *) field size specifier.

'.n'

Precision specifier.

':n'

Field size precision specifier.

';n'

Column width specifier.

'*'

If n is a * then next argument is used for precision/field size.

"'"

Set a pad string. ' cannot be a part of the pad string (yet).

'~'

Get pad string from argument list.

'<'

Use same argument again.

'^'

Repeat this on every line produced.

'@@'

Repeat this format for each element in the argument array.

'>'

Put the string at the bottom end of column instead of top.

'_'

Set width to the length of data.

'[n]'

Select argument number n. Use * to use the next argument as selector.


The following operators are supported:

'%'

Percent.

'b'

Signed binary integer.

'd'

Signed decimal integer.

'u'

Unsigned decimal integer.

'o'

Signed octal integer.

'x'

Lowercase signed hexadecimal integer.

'X'

Uppercase signed hexadecimal integer.

'c'

Character. If a fieldsize has been specified this will output the low-order bytes of the integer in network byte order.

'f'

Float. (Locale dependent formatting.)

'g'

Heuristically chosen representation of float. (Locale dependent formatting.)

'G'

Like %g, but uses uppercase E for exponent.

'e'

Exponential notation float. (Locale dependent output.)

'E'

Like %e, but uses uppercase E for exponent.

'F'

Binary IEEE representation of float (%4F gives single precision, %8F gives double precision.)

's'

String.

'O'

Any value, debug style. Do not rely on the exact formatting; how the result looks can vary depending on locale, phase of the moon or anything else the _sprintf method implementor wanted for debugging.

'n'

No operation (ignore the argument).

't'

Type of the argument.

'{'

Perform the enclosed format for every element of the argument array.

'}'

Most modifiers and operators are combinable in any fashion, but some combinations may render strange results.

If an argument is an object that implements lfun::_sprintf() , that callback will be called with the operator as the first argument, and the current modifiers as the second. The callback is expected to return a string.

Example

Pike v7.3 release 11 running Hilfe v2.0 (Incremental Pike Frontend) > int screen_width=70; Result: 70 > mixed sample; > write(sprintf("fish: %c\n", 65)); fish: A Result: 8 > write(sprintf("Hello green friends\n")); Hello green friends Result: 20 > write(sprintf("num: %d\n", 10)); num: 10 Result: 8 > write(sprintf("num: %+10d\n", 10)); num: +10 Result: 16 > write(sprintf("num: %010d\n", 5*2)); num: 0000000010 Result: 16 > write(sprintf("num: %|10d\n", 20/2)); num: 10 Result: 16 > write(sprintf("%|*s\n",screen_width,"THE NOT END")); THE NOT END Result: 71 > write(sprintf("%|=*s\n",screen_width, "fun with penguins\n")); fun with penguins Result: 71 > write(sprintf("%-=*O\n",screen_width,({ "fish", 9, "gumbies", 2 }))); ({ /* 4 elements */ "fish", 9, "gumbies", 2 }) Result: 426 > write(sprintf("%-=*s\n", screen_width, >> "This will wordwrap the specified string within the "+ >> "specified field size, this is useful say, if you let "+ >> "users specify their screen size, then the room "+ >> "descriptions will automagically word-wrap as appropriate.\n"+ >> "slosh-n's will of course force a new-line when needed.\n")); This will wordwrap the specified string within the specified field size, this is useful say, if you let users specify their screen size, then the room descriptions will automagically word-wrap as appropriate. slosh-n's will of course force a new-line when needed. Result: 355 > write(sprintf("%-=*s %-=*s\n", screen_width/2, >> "Two columns next to each other (any number of columns will "+ >> "of course work) independantly word-wrapped, can be useful.", >> screen_width/2-1, >> "The - is to specify justification, this is in addherence "+ >> "to std sprintf which defaults to right-justification, "+ >> "this version also supports centre and right justification.")); Two columns next to each other (any The - is to specify justification, number of columns will of course this is in addherence to std work) independantly word-wrapped, sprintf which defaults to can be useful. right-justification, this version also supports centre and right justification. Result: 426 > write(sprintf("%-$*s\n", screen_width, >> "Given a\nlist of\nslosh-n\nseparated\n'words',\nthis option\n"+ >> "creates a\ntable out\nof them\nthe number of\ncolumns\n"+ >> "be forced\nby specifying a\npresision.\nThe most obvious\n"+ >> "use is for\nformatted\nls output.")); Given a list of slosh-n separated 'words', this option creates a table out of them the number of columns be forced by specifying a presision. The most obvious use is for formatted ls output. Result: 312 > write(sprintf("%-#*s\n", screen_width, >> "Given a\nlist of\nslosh-n\nseparated\n'words',\nthis option\n"+ >> "creates a\ntable out\nof them\nthe number of\ncolumns\n"+ >> "be forced\nby specifying a\npresision.\nThe most obvious\n"+ >> "use is for\nformatted\nls output.")); Given a creates a by specifying a list of table out presision. slosh-n of them The most obvious separated the number of use is for 'words', columns formatted this option be forced ls output. Result: 312 > sample = ([ "align":"left", "valign":"middle" ]); Result: ([ /* 2 elements */ "align":"left", "valign":"middle" ]) > write(sprintf("<td%{ %s='%s'%}>\n", (array)sample)); <td valign='middle' align='left'> Result: 34 > write(sprintf("Of course all the simple printf options "+ >> "are supported:\n %s: %d %x %o %c\n", >> "65 as decimal, hex, octal and a char", >> 65, 65, 65, 65)); Of course all the simple printf options are supported: 65 as decimal, hex, octal and a char: 65 41 101 A Result: 106 > write(sprintf("%[0]d, %[0]x, %[0]X, %[0]o, %[0]c\n", 75)); 75, 4b, 4B, 113, K Result: 19 > write(sprintf("%|*s\n",screen_width, "THE END")); THE END Result: 71

See also

lfun::_sprintf()