Solved

text from text file to be converted to integers and letters

Posted on 2004-05-02
3
165 Views
Last Modified: 2010-04-05
I have written data to a text file.  Here is an example:
red341.
I am wanting to get this data back from the text file and split this string up into two different variables.
red in one string variable and 341 into an integer variable.
How do I go about this in Delphi 7?
thanks, Rob
0
Comment
Question by:robertguy
  • 2
3 Comments
 
LVL 7

Expert Comment

by:sftweng
ID: 10970683
You could do something like this:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, StdCtrls;

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Panel1: TPanel;
    Panel2: TPanel;
    procedure Edit1Change(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Edit1Change(Sender: TObject);
var
  s1, s2, s3 : string;
  i : integer;
begin
  s1 := Edit1.Text;
  s2 := '';
  s3 := '';
  FOR i := 1 TO Length(s1) DO
  BEGIN
    IF (s1[i] IN ['0'..'9'])
    THEN s3 := s3+s1[i]
    ELSE s2 := s2+s1[i];
  END {FOR};
  panel1.Caption := s2;
  panel2.Caption := s3;
end;

end.
0
 
LVL 7

Accepted Solution

by:
sftweng earned 125 total points
ID: 10970687
Oh yes, finally, you could do a string to integer conversion like:

  i := StrToInt(s3);
0
 
LVL 12

Expert Comment

by:Ivanov_G
ID: 10970718
procedure TForm1.Button1Click(Sender: TObject);
var
  strings      : TStringList;
  counter      : Integer;
  strLine      : String;
  varString    : String;
  varInteger   : Integer;
  linec        : Integer;
begin
  // create the string and load from file
  strings := TStringList.Create;
  strings.LoadFromFile('C:\test.txt');
  // loop through the strings
  for counter := 0 to strings.Count - 1 do
    begin
      strLine := Strings[counter];
      // pos indicates where digits start
      linec   := 0;
      repeat
        Inc(linec);
      until (strLine[linec] in ['0'..'9']);
      varString  := Copy(strLine, 1, linec-1);
      varInteger := StrToInt(Copy(strLine, linec, length(strLine) - linec + 1));
      // add to string grid
      StringGrid1.Cells[0, counter] := varString;
      StringGrid1.Cells[1, counter] := IntToStr(varInteger);
    end;
end;
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

820 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