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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

pad string with blank space 2

Status
Not open for further replies.

AP81

Programmer
Apr 11, 2003
740
AU
Is there a way to pad a string with empty space?, e.g. say I want all strings to be set as 100 characters, then 'hello' will appear as 'hello' + 95 trailing characters of blank space.




------------------------------------
There's no place like 127.0.0.1
------------------------------------
 
Yes, this is fairly easy to do with the StringOfChar function
Code:
  s := 'hello';
  s := s + StringOfChar ( ' ', 100 - Length(s) );


Andrew
Hampshire, UK
 
Just what I needed. Thanks




------------------------------------
There's no place like 127.0.0.1
------------------------------------
 
You may also want to look into using the Format function. While it seems overly complex at first, it can actually simplify huge chunks of code once you get your head around it.

For your question you could use

Code:
s := 'hello';
s := Format('%-100s', [s]);

Have a look at the Format function in the help, and follow the link to the Format Strings topic. Format has a lot of support for formatting numbers as well.
 
whoops - the code I just posted adds the spaces before the text, if you want it trailing just use:

Code:
s := Format('%100s', [s]);
 
You're right, it takes some getting use to, but the format function is great! I use it for a text file that I send to an outside agency and it has to be in a fixed format.

In my procedure I declared a constant with the format string:

Code:
So this formats the first 26 items in my text file:
const headerstringformat = '%-2s%-3s%-11s%-2s%-2s%-2s%-2s%-2s%-2s%-4s%14s%-14s%-4s%-3s%-4s%-2s%-9s%-4s%-4s%-2s%-4s%-2s%-8s%-4s%-4s%-27s';

Format(headerstringformat, ['item1', 'item2', 'item3, ... 'item26'])







Leslie
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top