Split Edit1.Text to 3 other edit boxes

Pika
Pika used Ask the Experts™
on
Hi,

How can I split Edit1.Text in 3 pieces and then put piece #1 into Edit2.Text, piece #2 into Edit3.Text and piece #4 into Edit4.Text?

For example:

Edit1.Text is '123456789'

Then I want Edit2.Text to be '123', Edit3.Text to be '456' and Edit4.Text to be '789'.

Example number 2:

Edit1.Text is '123+456+789'

Then I want Edit2.Text to be '123', Edit3.Text to be '456' and Edit4.Text to be '789'. So here I want the '+' to be gone.

Thx in Advance!
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2004

Commented:
a sample

unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Edit2: TEdit;
    Edit3: TEdit;
    Edit4: TEdit;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

function splitstr(var astring : String; Delimiter : String) : String;
var
  p : Integer;
begin
  result := '';
  if AString <> '' then
  begin
    p := pos(Delimiter,AString);
    if p > 0 then
    begin
      result := copy(AString,1,p-1);
      AString := copy(AString,p+length(Delimiter),maxLongInt);
    end
    else
    begin
      result := AString;
      AString := '';
    end;
  end;
end;


procedure TForm1.Button1Click(Sender: TObject);
var s : string;
begin
  s := edit1.Text;
  edit2.Text := splitstr(s,'+');
  edit3.Text := splitstr(s,'+');
  edit4.Text := splitstr(s,'+');
end;

end.

meikl ;-)
Top Expert 2004
Commented:
oops, for the first one

edit2 := copy(edit1.text,1,3);
edit3 := copy(edit1.text,4,3);
edit4 := copy(edit1.text,7,3);

meikl ;-)

Author

Commented:
okay, they both work ;-)
thx a lot!
rated: Excellent, cuz i can just copy/paste and ready :-)
thx!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial