xdelph,
Cita:
Empezado por xdelph
...linea es el string y contiene : "1|aaaaaa|2000|Juan Pérez"...necesito guardar en variables el 2000 y "Juan Pérez"...soy casi nuevo en Delphi...
|
Revisa este código:
Código Delphi
[-]
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, StrUtils;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
public
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
Source : String;
S1, S2, AuxStr : String;
SL : TStringList;
i : Integer;
P : Integer;
MsgUsr, MsgApp : String;
begin
Source := '1|aaaaaa|2000|Juan Pérez';
SL := TStringList.Create;
ExtractStrings(['|'],[],PChar(Source),SL);
P := SL.IndexOf('2000');
if P <> - 1 then
S1 := SL[P];
P := SL.IndexOf('Juan Pérez');
if P <> - 1 then
S2 := SL[P];
MsgApp := 'Método 1 (ExtractStrings-IndexOf)';
MsgUsr := Format('S1 = %s y S2 = %s',[S1,S2]);
MessageBox(Handle, PChar(MsgUsr), PChar(MsgApp), MB_OK + MB_ICONINFORMATION);
SL.Free;
S1 := EmptyStr;
S2 := EmptyStr;
i := 1;
repeat
P := PosEx('|', Source,i);
if P <> 0 then
begin
AuxStr := Copy(Source,i,P-i);
if (AuxStr = '2000') then
S1 := AuxStr;
if (AuxStr = 'Juan Pérez') then
S2 := AuxStr;
i := P+1;
end
else
begin
AuxStr := Copy(Source,i,MaxInt);
if (AuxStr = '2000') then
S1 := AuxStr;
if (AuxStr = 'Juan Pérez') then
S2 := AuxStr;
end;
until (P = 0);
MsgApp := 'Método 2 (PosEX-Copy)';
MsgUsr := Format('S1 = %s y S2 = %s',[S1,S2]);
MessageBox(Handle, PChar(MsgUsr), PChar(MsgApp), MB_OK + MB_ICONINFORMATION);
S1 := EmptyStr;
S2 := EmptyStr;
SL := TStringList.Create;
ExtractStrings(['|'],[],PChar(Source),SL);
for i := 0 to SL.Count - 1 do
begin
if SL[i] = '2000' then
S1 := SL[i];
if SL[i] = 'Juan Pérez' then
S2 := SL[i];
if (S1 <> EmptyStr) and (S2 <> EmptyStr) then
Break;
end;
MsgApp := 'Método 3 (ExtractStrings-Count)';
MsgUsr := Format('S1 = %s y S2 = %s',[S1,S2]);
MessageBox(Handle, PChar(MsgUsr), PChar(MsgApp), MB_OK + MB_ICONINFORMATION);
S1 := EmptyStr;
S2 := EmptyStr;
i := 1;
for P := 1 to Length(Source) do
begin
if Source[P] = '|' then
begin
AuxStr := Copy(Source,i,P-i);
if (AuxStr = '2000') then
S1 := AuxStr;
if (AuxStr = 'Juan Pérez') then
S2 := AuxStr;
i := P + 1;
end;
end;
AuxStr := Copy(Source,i,MaxInt);
if (AuxStr = '2000') then
S1 := AuxStr;
if (AuxStr = 'Juan Pérez') then
S2 := AuxStr;
MsgApp := 'Método 4 (String-Length)';
MsgUsr := Format('S1 = %s y S2 = %s',[S1,S2]);
MessageBox(Handle, PChar(MsgUsr), PChar(MsgApp), MB_OK + MB_ICONINFORMATION);
end;
end.
El código anterior en Delphi 7 sobre Windows 7 Professional x32,
Ejemplifica la extracción de substrings de un String por 4 diferentes métodos, como se muestra en la siguiente imagen:
Notas:
1- El código propuesto tiene como único objetivo,
servir de guía a los requerimientos planteados en el
Msg #1.
2- Es importante
que estudies los métodos propuestos y hagas pruebas con los mismos para internalizar los conceptos,
programar se aprende programando.
Revisa esta información:
Espero sea útil
Nelson.