Solved

text from text file to be converted to integers and letters

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
System restore point 4 91
Send message from delphi to whatsapp, is it possible? 3 1,178
control image tags in a string ? 12 111
Need Help Delphi 2010 CheckBox1 Stored value in memo 13 57
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

895 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

14 Experts available now in Live!

Get 1:1 Help Now