Solved

text from text file to be converted to integers and letters

Posted on 2004-05-02
3
164 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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

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…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

810 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