Club Delphi  
    Paypal   FTP   CCD     Buscar   Trucos   Trabajo   Foros

Retroceder   Foros Club Delphi > Principal > Varios
Registrarse FAQ Miembros Calendario Guía de estilo Buscar Temas de Hoy Marcar Foros Como Leídos

Coloboración Paypal con ClubDelphi

 
 
Herramientas Buscar en Tema Desplegado
  #9  
Antiguo 09-06-2015
Avatar de nlsgarcia
[nlsgarcia] nlsgarcia is offline
Miembro Premium
 
Registrado: feb 2007
Ubicación: Caracas, Venezuela
Posts: 2.206
Poder: 23
nlsgarcia Tiene un aura espectacularnlsgarcia Tiene un aura espectacular
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
    { Private declarations }
  public
    { Public declarations }
  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

   // Obtener '2000' y 'Juan Pérez' de Source

   Source := '1|aaaaaa|2000|Juan Pérez';

   // Método-1

   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;

   // Método-2

   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);

   // Método-3

   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);

   // Método-4

   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.
Responder Con Cita
 


Herramientas Buscar en Tema
Buscar en Tema:

Búsqueda Avanzada
Desplegado

Normas de Publicación
no Puedes crear nuevos temas
no Puedes responder a temas
no Puedes adjuntar archivos
no Puedes editar tus mensajes

El código vB está habilitado
Las caritas están habilitado
Código [IMG] está habilitado
Código HTML está deshabilitado
Saltar a Foro

Temas Similares
Tema Autor Foro Respuestas Último mensaje
Punto de separación de miles m.ruiz Varios 5 22-11-2007 17:34:00
Separacion entre líneas de un QRMemo fedeloko Impresión 0 30-10-2006 19:53:57
Separación de QRDBText fedeloko Impresión 0 18-09-2006 17:44:19
file of, caracter de separación jonmendi Varios 12 09-06-2006 17:27:19
mainmenu y barritas de separación unreal4u Varios 2 22-04-2005 21:19:46


La franja horaria es GMT +2. Ahora son las 15:06:46.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi
Copyright 1996-2007 Club Delphi