Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Removing Trailing Spaces In Printf For A String

Status
Not open for further replies.

mark1110

Programmer
Apr 20, 2005
85
US
Is there a way to use printf to trim the trailing spaces. If I have an expression like:

printf '<%-10s>', "Hello";

It prints "Hello " with 5 trailing spaces.

I would like to print something like this:

"Hello"

without the trailing spaces. I tried using a trim function I found by googling trim spaces perl with no luck. Is there another way of doing this.

 
The "%-10s" forces 10 spaces.
If you don't want them then why use printf?
You could do this for example:
Code:
print '<', 'Hello', '>';
print '<', $variable, '>';


Trojan.
 
hehehe..... thats funny. You tell printf to add spaces on the right if necessary to pad a string to ten places, but then you don't want the spaces! Try not using printf and just use print.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Thank you for you help. I am very new to PERL and it has been a long time since I used the printf statement. The print statement did the trick.
 
Alternatively, just remove the formatting from the printf statement...

Code:
printf '<%s>', "Hello";
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top