Solved

how pad you pad a string with leading spaces?

Posted on 1998-12-04
9
4,088 Views
Last Modified: 2012-05-04
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
Comment
Question by:waipahu
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 4

Expert Comment

by:erajoj
ID: 1349304
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
 
LVL 35

Expert Comment

by:TimYates
ID: 1349305
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 Comment

by:waipahu
ID: 1349306
I got it!
I used TOPAZ LPAD to do the left pad with spaces!
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
 
LVL 4

Expert Comment

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

/// John
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 35

Expert Comment

by:TimYates
ID: 1349308
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
 
LVL 35

Expert Comment

by:TimYates
ID: 1349309
Heh...
sorry about that...
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
 
LVL 2

Expert Comment

by:lortega
ID: 1349310
KISS concept...

function PadLString(mString :String; Spaces :Integer):String;
begin
   result := StringOfChar(' ', Spaces) + Trim(mString)
end;

function PadRString(mString :String; Spaces :Integer):String;
begin
   result := Trim(mString) + StringOfChar(' ', Spaces);
end;


good luck,
luis
0
 

Accepted Solution

by:
teechong earned 10 total points
ID: 1349311
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 Comment

by:waipahu
ID: 1349312
Thanks for everybody who contributed!!!
I really appreciate it a lot!
This website is the best!
This is very helpful!
Go public....make big bucks (IPO)...

bye

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

895 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now