• Status: Solved
• Priority: Medium
• Security: Public
• Views: 4632

I have a SSN variable
I want it fixed length of 9 with spaces padded in front.
example:
SSN 123       before
SSN       123 after
-----------------------
SSN 1234      before
SSN      1234 after

i guess i want it right justified with leading spaces.
anybody have any clue? Do I use LPAD???? if so, do i need
to find out the lenght of the SSN first?
thanks
0
waipahu
• 3
• 2
• 2
• +2
1 Solution

Commented:
Is something like this what you want?:

function PadString(str: string; len:Integer; ch: Char):string;
begin
Result := '';
for iIndex := 1 to len-Length(str) do
AppendStr(Result, ch);
AppendStr(Result, str);
end;

/// John
0

Commented:
Or : (a bit faster)

function PadString(str: string; len:Integer; ch: Char):string;
begin
result := Trim( str ) ;
while( Length( str ) < len ) do
result := ch + result ;
end;

0

Author Commented:
I got it!
THank you all!

Can delphi be web enabled?  How would you do it?
Do i need a webserver of some sort?
Do I need the client-server version of delphi?

0

Commented:
Tim, could you please tell me in what way your code is faster?
Have you tested it? I don't think so.

/// John
0

Commented:
Hmmm, thought it should be, due to one less assign being required...  I'll test it now...

It might have been a fraudulent assumption *grin*

Tim.
0

Commented:
Heh...
yours was faster than mine...

Hmmm...what can I say....

I apologise for my big mouth...

And here is an _even faster_ bit of code to make up for it :)

function MyPadString( str : string; len : Integer; ch : Char ):string;
var
p : pChar ;
l, ll : longint ;
begin
p := allocMem( len + 1 ) ;
FillChar( p^, len, ch ) ;
l := len - Length( str ) ;
ll := Length( str ) ;
if( ll > len ) then ll := len ;
if( l < 0 ) then l := 0 ;
CopyMemory( pChar( integer( p ) + l ), @str[ 1 ], ll ) ;
result := strpas( p ) ;
Freemem( p, len + 1 ) ;
end;

Now, that _is_ fast :)

Cheers, and sorry again,

Tim.
0

Commented:
KISS concept...

begin
result := StringOfChar(' ', Spaces) + Trim(mString)
end;

begin
result := Trim(mString) + StringOfChar(' ', Spaces);
end;

good luck,
luis
0

Commented:
Don't you think using Format will do the job?
Format ('SSN %9d', [ssnnum] );

Of course, your labels or controls should use a fixed pitch font for proper display.
0

Author Commented:
Thanks for everybody who contributed!!!
I really appreciate it a lot!
This website is the best!
Go public....make big bucks (IPO)...

bye

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.