Ver Mensaje Individual
  #4  
Antiguo 20-03-2013
Avatar de ecfisa
ecfisa ecfisa is offline
Moderador
 
Registrado: dic 2005
Ubicación: Tres Arroyos, Argentina
Posts: 10.508
Reputación: 36
ecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to behold
Hola.

Otra alternativa para obtener un determinado campo:
Código Delphi [-]
function GetTextField(const SearchedText: string; SList: TStrings;
  const InxRetField: Integer; const Sep: Char): string;
var
  Aux  : TStrings;
  i    : Integer;
  Found: Boolean;
begin
  Result := '';
  i      := 0;
  Found  := False;
  while not Found and (i < SList.Count) do
  begin
    Aux := TStringList.Create;
    try
      Aux.Delimiter     := Sep;
      Aux.DelimitedText := SList[i];
      Found := Aux.IndexOf(SearchedText) <> -1;
      if Found then
        Result := Aux[InxRetField];
    finally
      Aux.Free;
    end;
    Inc(i);
  end
end;

Lamentablemente algunas versiones antiguas de Delphi (como la que tengo) adolecen de un bug. Y este es, que sea cual fuere el separador especificado, incluye siempre el espacio (' ') como tal, y por tanto no funcionará.
Pero aplicando algunas modificaciones que reemplacen los espacios por un caracter que sea inexistente en los datos, se puede lograr "esquivar" el bug:
Código Delphi [-]
// En el ejemplo mi caracter inexistente será  '~' (126)
function GetTextField(const SearchedText: string; SList: TStrings;
  const InxRetField: Integer; const Sep: Char): string;
const
  NONEXISTCHAR = #126;
var
  Aux: TStrings;
  i  : Integer;
  Found: Boolean;
begin
  Result := '';
  i      := 0;
  Found  := False;
  while not Found and (i < SList.Count) do
  begin
    Aux := TStringList.Create;
    try
      Aux.Delimiter     := Sep;
      Aux.DelimitedText := StringReplace(SList[i], ' ', NONEXISTCHAR , [rfReplaceAll]);
      Found := Aux.IndexOf(StringReplace(SearchedText, ' ', NONEXISTCHAR, [rfReplaceAll])) <> -1;
      if Found then
        Result := StringReplace(Aux[InxRetField], NONEXISTCHAR, ' ', [rfReplaceAll]);
    finally
      Aux.Free;
    end;
    Inc(i);
  end
end;

Llamada de ejemplo (para ambos casos):
Código Delphi [-]
var
  List: TStrings;
begin
  List := TStringList.Create;
  try
    List.LoadFromFile('C:\LINEAS.TXT');
    Edit2.Text := GetTextField(Edit1.Text, List, 3, '|');
  finally
    List.Free;
  end;
end;
El argumento InxRetField tiene como cota inferior cero y como superior el número de campos -1 (para tu caso sería 11-1).

Saludos.
__________________
Daniel Didriksen

Guía de estilo - Uso de las etiquetas - La otra guía de estilo ....
Responder Con Cita