Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Padding a string?

Posted on 2000-02-13
4
Medium Priority
?
489 Views
Last Modified: 2010-03-05
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
0
Comment
Question by:dankarran
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 19

Accepted Solution

by:
Kim Ryan earned 150 total points
ID: 2517253
# If you want padding after your variable:
$padded = sprintf("%-20.20s",$var);
For left justified padding, remove the - after the % sign
0
 
LVL 84

Expert Comment

by:ozo
ID: 2517260
substr($var. "             ",0,20) #is something like that
#or you might use something like
printf "%-20s",$var;
0
 
LVL 5

Expert Comment

by:PC_User321
ID: 2517269
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
 

Author Comment

by:dankarran
ID: 2518060
sorry guys, i'd like to give you all credit but teraplane came up with the answer first
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question