FAQ 4.33 How do I pad a string with blanks or pad a number with zeroes?

P

PerlFAQ Server

This is an excerpt from the latest version perlfaq4.pod, which
comes with the standard Perl distribution. These postings aim to
reduce the number of repeated questions as well as allow the community
to review and update the answers. The latest version of the complete
perlfaq is at http://faq.perl.org .

--------------------------------------------------------------------

4.33: How do I pad a string with blanks or pad a number with zeroes?

In the following examples, $pad_len is the length to which you wish to
pad the string, $text or $num contains the string to be padded, and
$pad_char contains the padding character. You can use a single character
string constant instead of the $pad_char variable if you know what it is
in advance. And in the same way you can use an integer in place of
$pad_len if you know the pad length in advance.

The simplest method uses the "sprintf" function. It can pad on the left
or right with blanks and on the left with zeroes and it will not
truncate the result. The "pack" function can only pad strings on the
right with blanks and it will truncate the result to a maximum length of
$pad_len.

# Left padding a string with blanks (no truncation):
$padded = sprintf("%${pad_len}s", $text);
$padded = sprintf("%*s", $pad_len, $text); # same thing

# Right padding a string with blanks (no truncation):
$padded = sprintf("%-${pad_len}s", $text);
$padded = sprintf("%-*s", $pad_len, $text); # same thing

# Left padding a number with 0 (no truncation):
$padded = sprintf("%0${pad_len}d", $num);
$padded = sprintf("%0*d", $pad_len, $num); # same thing

# Right padding a string with blanks using pack (will truncate):
$padded = pack("A$pad_len",$text);

If you need to pad with a character other than blank or zero you can use
one of the following methods. They all generate a pad string with the
"x" operator and combine that with $text. These methods do not truncate
$text.

Left and right padding with any character, creating a new string:

$padded = $pad_char x ( $pad_len - length( $text ) ) . $text;
$padded = $text . $pad_char x ( $pad_len - length( $text ) );

Left and right padding with any character, modifying $text directly:

substr( $text, 0, 0 ) = $pad_char x ( $pad_len - length( $text ) );
$text .= $pad_char x ( $pad_len - length( $text ) );



--------------------------------------------------------------------

The perlfaq-workers, a group of volunteers, maintain the perlfaq. They
are not necessarily experts in every domain where Perl might show up,
so please include as much information as possible and relevant in any
corrections. The perlfaq-workers also don't have access to every
operating system or platform, so please include relevant details for
corrections to examples that do not work on particular platforms.
Working code is greatly appreciated.

If you'd like to help maintain the perlfaq, see the details in
perlfaq.pod.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top