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
  #6  
Antiguo 28-06-2022
Avatar de movorack
[movorack] movorack is offline
Miguel A. Valero
 
Registrado: feb 2007
Ubicación: Bogotá - Colombia
Posts: 1.346
Poder: 22
movorack Va camino a la famamovorack Va camino a la fama
Creo que es mejor hacer el overload de cada Split

Aunque lo hice con fines "didacticos", pienso que se complica demasiado el código haciendo el split en una sola función.

Código Delphi [-]
unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, System.Rtti;

type
  PArrInt = ^TArrInt;
  TArrInt = TArray< Integer >;
  PArrDbl = ^TArrDbl;
  TArrDbl = TArray< Double >;
  PArrStr = ^TArrStr;
  TArrStr = TArray< String >;

  TForm1 = class(TForm)
    Button1: TButton;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    function Split(const S: string; const Separator: array of char): T;
    function toArrStr(const A: TArrInt): TArrStr; overload; inline;
    function toArrStr(const A: TArrDbl): TArrStr; overload; inline;
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

{ TForm1 }

function TForm1.Split(const S: string; const Separator: array of char): T;
var
  i: Integer;
  lArrStr: TArrStr;
  lArrInt: TArrInt;
  lTmpI: Integer;
  lArrDbl: TArrDbl;
  lTmpD: Double;
begin
  if (TypeInfo(T) <> TypeInfo(TArrInt))
    and (TypeInfo(T) <> TypeInfo(TArrDbl))
    and (TypeInfo(T) <> TypeInfo(TArrStr))
  then
    raise Exception.Create('¡Tipo generico no manejado!');

  lArrStr := S.Split(Separator);

  if TypeInfo(T) = TypeInfo(TArrInt) then
  begin
    SetLength(lArrInt, 0);

    for i := 0 to High(lArrStr) do
    begin
      if not TryStrToInt(lArrStr[i], lTmpI) then
        Continue;

      SetLength(lArrInt, Length(lArrInt)+1);
      lArrInt[Length(lArrInt)-1] := lTmpI;
    end;

    PArrInt(@Result)^ := lArrInt;
  end
  else
  if TypeInfo(T) = TypeInfo(TArrDbl) then
  begin
    SetLength(lArrDbl, 0);

    for i := 0 to High(lArrStr) do
    begin
      if not TryStrToFloat(lArrStr[i], lTmpD) then
        Continue;

      SetLength(lArrDbl, Length(lArrDbl)+1);
      lArrDbl[Length(lArrDbl)-1] := lTmpD;
    end;

    PArrDbl(@Result)^ := lArrDbl;
  end
  else
  if TypeInfo(T) = TypeInfo(TArrStr) then
    PArrStr(@Result)^ := lArrStr;
end;

function TForm1.toArrStr(const A: TArrInt): TArrStr;
var
  i: Integer;
begin
  for i := 0 to High(A) do
  begin
    SetLength(Result, Length(Result)+1);
    Result[Length(Result)-1] := IntToStr(A[i]);
  end;
end;

function TForm1.toArrStr(const A: TArrDbl): TArrStr;
var
  i: Integer;
begin
  for i := 0 to High(A) do
  begin
    SetLength(Result, Length(Result)+1);
    Result[Length(Result)-1] := FloatToStr(A[i]);
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
const
  _INT = '1;100;1000;10000;100000';
  _DBL = '1,1;1,2;1,3;1,4;1,5';
  _STR = 'A;B;C;D;E';
var
  lArrI: TArrInt;
  lArrD: TArrDbl;
  lArrS: TArrStr;
begin
  lArrI := Split< TArrInt >(_INT, [';']);
  lArrD := Split< TArrDbl >(_DBL, [';']);
  lArrS := Split< TArrStr >(_STR, [';']);

  Memo1.Lines.Clear;
  Memo1.Lines.AddStrings(toArrStr(lArrI));
  Memo1.Lines.AddStrings(toArrStr(lArrD));
  Memo1.Lines.AddStrings(lArrS);
end;

end.

Resultado:

Código:
1
100
1000
10000
100000
1,1
1,2
1,3
1,4
1,5
A
B
C
D
E
__________________
Buena caza y buen remar... http://mivaler.blogspot.com

Última edición por movorack fecha: 28-06-2022 a las 16:37:23.
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
Función que retorne Tarray<T> movorack OOP 2 23-09-2021 15:30:39
Pasar de string a $ o de $ a string BuRtOn Varios 8 17-06-2008 01:53:45
la expresión String s = new String("hola"); David JAVA 4 22-02-2008 19:33:20
(const Value: string) vs (Value: string) eliash OOP 10 14-12-2005 19:10:13
String de mas de 250? unko! Varios 5 28-03-2005 17:55:19


La franja horaria es GMT +2. Ahora son las 08:21:17.


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