Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

how pad you pad a string with leading spaces?

Posted on 1998-12-04
9
4,133 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
add combobox item based on numbers 9 154
How to define IfThen functions in one common unit? 4 60
SUM 2 INTEGER ARRAYS INTO 1 10 108
how to change, disabled button color FMX ? 1 40
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…
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…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

856 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