How to display strings
Strings are formatted in the following sequence:
"%" Percent sign is always first
"-" Code a minus sign to left-justify
# Number that specifies the total field width
.# Use only to truncate the input string
s Follow with an "s" to specify a string.
Some examples:
X = sprintf("%-20s", "Jello, curdled");
assigns the string:
012345678901234567890
"Jello, curdled "
to the variable X. There are 6 spaces padded on the
end, because the total length of the field is 20. Note
that I've used a column ruler to emphasize the field length.
X = sprintf("%20s", "Jello, curdled");
assigns the string:
012345678901234567890
" Jello, curdled"
to the variable X. The field was
right-justified by default, because there was no
minus sign after the percent sign.
You can also truncate the string by adding the period and specifier
for the input string length.
X = sprintf("%15s", "Jello, hurled!");
assigns the value " Jello, hurled!" to X (note that
"Jello, hurled!" is 14 characters, and because there was no minus
sign after the percent sign, the field was
right-justified.
But:
X = sprintf("%-12.11s", "Jello, hurled!");
produces "Jello, hurl ". This is because 3 characters were
stripped off the input string, and it was left-aligned in a
12-character field. So there is exactly
one space after "hurl".
Finally, you sometimes need to pad zeros on the left of a string
field (for things like customer numbers, which aren't really
numbers).
To do this, use a 0 instead of a minus sign after the percent
sign (the result is always right-justified, with the padded
zeros).
For example:
X = sprintf("%015.11s", "Jello, hurled!");
produces: "0000Jello, hurl"
and
X = sprintf("%015", "Jello, hurled!");
produces: "0Jello, hurled!".
Return
to local table of contents
Return
to global table of contents
Frames mode, or
No frames