• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3237
  • Last Modified:

PRINTF Syntax for centering a string

I am using PRINTF to format some output.  I know it is possible to justify left and right:

printf '%9s',   "test";   # prints "     test"
printf '%-6s',  "test";   # prints "test     "

My question:  how can I center the string?    i.e. "   test   "

0
cs76737
Asked:
cs76737
1 Solution
 
TalmashCommented:
my $string = "test";
$len = len($string);
my $range = 20;
my $space = int((20-$len)/2);
print " " x $space.$string." " x $space;
0
 
cs76737Author Commented:
Talmash --

thank you for the suggestion.  Again, I need to do this within the PRINTF function for my program to work.  

Is there a way to do it using PRINTF?
0
 
Adam314Commented:
there is no format specifier available in printf to center.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now