Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

how pad you pad a string with leading spaces?

Posted on 1998-12-04
9
Medium Priority
?
4,350 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
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.

 
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
 
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 40 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

[Webinar] Cloud Security

In this webinar you will learn:

-Why existing firewall and DMZ architectures are not suited for securing cloud applications
-How to make your enterprise “Cloud Ready”, and fix your aging DMZ architecture
-How to transform your enterprise and become a Cloud Enabler

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Integration Management Part 2
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

972 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