Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > FAQ 4.33 How do I pad a string with blanks or pad a number with zeroes?

Reply
Thread Tools

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

 
 
PerlFAQ Server
Guest
Posts: n/a
 
      02-05-2011
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
FAQ 5.19 How can I open a file with a leading ">" or trailing blanks? PerlFAQ Server Perl Misc 0 03-27-2011 04:00 AM
FAQ 4.33 How do I pad a string with blanks or pad a number with zeroes? PerlFAQ Server Perl Misc 0 03-13-2011 05:00 AM
FAQ 5.19 How can I open a file with a leading ">" or trailing blanks? PerlFAQ Server Perl Misc 0 02-02-2011 11:00 PM
Pyrex: problem with blanks in string Hans Terlouw Python 1 06-04-2007 03:23 PM



Advertisments