Einzelnen Beitrag anzeigen

Fuchtel

Registriert seit: 9. Nov 2005
Ort: Bamberg
53 Beiträge
 
Delphi 2005 Personal
 
#10

AW: Nur eine Spalte aus CSV-Datei auslesen und verarbeiten

  Alt 23. Nov 2020, 18:48
Meine Lösung:

Eine von TStringList abgeleitete Komponente (z,B,.: TStringList_Ex);

Uberschreibe
Code:
function Add(const S: string): Integer;
var
  sp: Szring;
begin
  Result := GetCount;
  sp := Part(S, <cSep>, 2);
  Insert(Result, sp);
end;
Wobei cSep dein Separator (hier #09 ist)


Code:
function Part(const sVar: String; const cSep: Char; UV: Integer): String;
var
  P, S : PChar;
  i, j : Integer;
begin
  P := Pointer(sVar);
  S := P;
  i := 1;
  j := UV; Inc(j);
  If P <> nil then
    begin
      While (P^ <> #0) do
        begin
          If P^ = cSep then
            begin
              INC(i);
              If i = UV then S := P;
              If i = j then break;
            end;
          P := NextChar(P);
        end;
    If UV > 1 then INC(s);
    If (i >= UV) and (UV > 0) then
        System.SetString(Result, S, P - S)
      else
        Result := '';
  end;
end;
Falls NextChar fehlt:

Code:
function NextChar(P: PChar): PChar;
begin
  Result := P;
  if (Result <> nil) and (Result^ <> #0) then
  begin
    Inc(Result);
    {$IFDEF UNICODE}
    if Result^.IsLowSurrogate then
      Inc(Result);
    while Result^.GetUnicodeCategory = TUnicodeCategory.ucNonSpacingMark do
      Inc(Result);
    {$ENDIF}
  end;
end;
Wobei, wenn Du nur den größten Wert ermitteln willst, nimm nur daraus die Proceduren:

Code:
procedure TStrings.LoadFromFile(const FileName: string);
Code:
procedure TStrings.LoadFromStream(Stream: TStream; Encoding: TEncoding);
und fuer Add

Code:
function Add(const S: string): Integer;
var
  sp: String;
  i: Integer
begin
  sp := Part(S, <cSep>, 2);
  FMax := Max(FMax, StrToIntDef(sP, - MaxInt));
end;
  Mit Zitat antworten Zitat