[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Formatted String??

Posted on 2006-05-10
7
Medium Priority
?
184 Views
Last Modified: 2010-04-05
hi experts,

i have this vars:
empid = 6
name  = Tony Moon
date  = 16/07/2006
total = 782169.65

and i have standerd string:
IIIIIXXXXXXXXXXXXDDDDDDDDTTTTTTTTTTSSS

using the above vars, i want to create formatted string to be like this:
00006Tony MoonXXX160720060078216965XXX

00006=empid
Tony MoonXXX = name+3 spaces
16072006 = date
0060078216965 = total
XXX = 3spaces

** The result must be 35 Chars
0
Comment
Question by:mobsos
5 Comments
 
LVL 28

Accepted Solution

by:
2266180 earned 2000 total points
ID: 16647625
hm.. you have some errors in your request:
IIIIIXXXXXXXXXXXXDDDDDDDDTTTTTTTTTTSSS
is of length 38 not 35

here is my test case:

var s:string;
    empid:integer;
    name:string;
    date:string;
    total:integer;
begin
  empid:=6;
  name:='Tony Moon';
  date:='16072006';
  total:=78216965;
  s:=format('%.5d%-12s%8s%.10d%3s',[empid,name,date,total,'']);
  showmessage(s);
  showmessage(inttostr(length(s)));
end;

in order to get your example you must use:
s:=format('%.5d%-9s%8s%.10d%3s',[empid,name,date,total,'']);
0
 

Expert Comment

by:shiyunchi
ID: 16647662
Use Format and FormatDateTime.
0
 
LVL 12

Expert Comment

by:AmigoJack
ID: 16647691
lookup the delphi help for the function "format". the usage of it for your needs should look like this, but i havent tested it:

var
  s, name, date: string;
  empid: dword;
  total: double;
begin

  // please note from the declaration above on how i think what youre datatypes are.
  // this includes that the "date" is already present in the right string format.
  // if all your variables are strings, you only need %Xs syntax.

  s:= format('%05d%-12s%s%010d', empid, name, date, round(total* 100));
0
 
LVL 17

Expert Comment

by:mokule
ID: 16648077
it is unclear to me what type is date.
if it is string You could do.

Edit1.Text := Format('%5.5d%-12s%8s%10.10d   ',[empid,name,StringReplace(date,'/','',[rfreplaceAll]),round(100*total)]);
0
 
LVL 17

Expert Comment

by:mokule
ID: 16648112
or if a name can be longer than 12 chars and You want to include only 12 first chars from it

Edit1.Text := Format('%5.5d%-12.12s%8s%10.10d   ',[empid,name,StringReplace(date,'/','',[rfreplaceAll]),round(100*total)]);
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

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…
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…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

872 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