Solved

how pad you pad a string with leading spaces?

Posted on 1998-12-04
9
4,055 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
This video discusses moving either the default database or any database to a new volume.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

708 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

12 Experts available now in Live!

Get 1:1 Help Now