Padding a string?

How do I take the contents of a variable and print it out with a certain number of spaces to make it up to, say, 20 characters (by padding it with spaces)?

is it something like left($var . "             ", 20)


thanks
dankarranAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
Kim RyanConnect With a Mentor IT ConsultantCommented:
# If you want padding after your variable:
$padded = sprintf("%-20.20s",$var);
For left justified padding, remove the - after the % sign
0
 
ozoCommented:
substr($var. "             ",0,20) #is something like that
#or you might use something like
printf "%-20s",$var;
0
 
PC_User321Commented:
Try these.
(The plusses are just to show the start and end of the string).

$Str1  = "One";
$Str11 = "Eleven";

printf "+%-20s+\n", $Str1;
printf "+%-20s+\n", $Str11;

printf "+%20s+\n", $Str1;
printf ":%20s:\n", $Str11;

printf "+%s+\n", $Str1;
printf "+%s+\n", $Str11;
0
 
dankarranAuthor Commented:
sorry guys, i'd like to give you all credit but teraplane came up with the answer first
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.