|
|
|
Composing Strings with sprintfC programmers are familiar with the functions sprintf and sscanf, which are used to respectively create and analyze strings according to format specifiers. Pike had its own sprintf and sscanf, which are not entirely unlike those in C. If we start with sprintf, it builds a string according to a string with format specifiers, and a number of arguments. For example, if the variables x and y both contain the floating-point number 2.0, the expression sprintf("They are %.2f and %10.3f.", x, y);would give the result "They are 2.00 and 2.000." The format string consists of characters, that will be copied to the result. The format string can also contain "percent specifiers", which will be replaced by its arguments. For example, %d is used to mark an integer. Here is a list of some of the more common format specifiers:
There is a large number of modifiers that you can use to control the format of the output. You insert these modifiers between the percent sign and the format character. Here are some examples of the most useful ones:
C programmers should note that a difference from the sprintf you have in C is that Pike's sprintf returns the string as the value of the call. |
|||||||||||||||||||||||||||||||||||||||||||||