Registriert seit: 6. Apr 2011
Ort: Berlin
3.070 Beiträge
Delphi 10.4 Sydney
|
AW: Generische string.Split Funktion
27. Sep 2018, 13:34
Delphi-Quellcode:
type
TArrayHelper = record
public
class function Explode<T>(const ADelimiter, AString: string): TArray<T>; static;
end;
class function TArrayHelper.Explode<T>(const ADelimiter, AString: string): TArray<T>;
var
LArr: TArray<string>;
LIntArr: TArray<Integer>;
Li: Integer;
I: Integer;
begin
SetLength(Result, 0);
LArr := AString.Split([ADelimiter]);
for Li := 0 to Length(LArr) - 1 do
begin
if (TypeInfo(T) = TypeInfo(string)) then
begin
Result := TArray<T>(LArr);
end
else if (TypeInfo(T) = TypeInfo(Integer)) then
begin
SetLength(LIntArr, Length(LArr));
for I := Low(LIntArr) to High(LIntArr) do
begin
LIntArr[I] := LArr[I].ToInteger;
end;
Result := TArray<T>(LIntArr);
end;
end;
end;
procedure Test;
var
MyString: string;
MyStringArray: TArray<string>;
MyIntegerArray: TArray<Integer>;
begin
MyString := '1,2,3';
MyStringArray := TArrayHelper.Explode<string>(',', MyString);
MyIntegerArray := TArrayHelper.Explode<Integer>(',', MyString);
end;
|
|
Zitat
|