Solved

how pad you pad a string with leading spaces?

Posted on 1998-12-04
9
4,232 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
[X]
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
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses
Course of the Month9 days, 15 hours left to enroll

623 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