Solved

text from text file to be converted to integers and letters

Posted on 2004-05-02
3
169 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
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…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses
Course of the Month11 days, 5 hours left to enroll

628 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