Solved

text from text file to be converted to integers and letters

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

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…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

707 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

18 Experts available now in Live!

Get 1:1 Help Now