Solved

break 2 element string into int and string

Posted on 1999-01-12
3
149 Views
Last Modified: 2010-04-06
Reading a line from file that has two elements, first bit of data is price, and 2nd is part description.  These two elements are separated by spaces.  How do I grab the first piece of the line and convert to integer? Example line record below.
 $100.00     Range Hood
0
Comment
Question by:hoffb
3 Comments
 
LVL 20

Accepted Solution

by:
Madshi earned 20 total points
Comment Utility
Try this:

function ConvertToPriceAndDescr(str: string; var price: integer; var descr: string) : boolean;
begin
  result:=false;
  Trim(str);
  i1:=Pos(' ',str);
  descr:=Copy(str,i1,maxInt);
  Trim(descr);
  Delete(str,i1,maxInt);
  i1:=1;
  while (i1<=length(str)) and (not (str[i1] in ['0'..'9'])) do
    inc(i1);
  Delete(str,1,i1-1);
  i1:=Pos('.',str);
  try
    price:=StrToInt(Copy(str,1,i1-1))*100 + StrToInt(Copy(str,i1+1,maxInt));
  except exit end;
  result:=true;
end;

In your example the result would be: "price=10000" and "descr='Range Hood'". To get the cents, devide price with 100.

Regards, Madshi.
0
 
LVL 5

Expert Comment

by:heathprovost
Comment Utility
Damn, beat me to it.... Yours is better anyway so I wont bother :)

Heath
0
 

Author Comment

by:hoffb
Comment Utility
I'll try it.  Fast Answer!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

772 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

13 Experts available now in Live!

Get 1:1 Help Now