Thema: Delphi Strings laden

Einzelnen Beitrag anzeigen

Benutzerbild von Jens Schumann
Jens Schumann

Registriert seit: 27. Apr 2003
Ort: Bad Honnef
1.644 Beiträge
 
Delphi 2009 Professional
 
#4

Re: Strings laden

  Alt 14. Dez 2005, 18:42
Hallo,
ich arbeite auch mit D5. Da es in D5 den Delimiter nicht gibt habe ich TStringList damit nachgerüstet.
Delphi-Quellcode:
unit JsStringListExt;

interface

uses SysUtils, Windows, classes;

Type

  TJsStringListExt = class(TStringList)
  private
    FDelimiter: Char;
    function GetCommaText: string;
    procedure SetCommaText(const Value: string);
  public
    constructor Create;
    property Delimiter : Char read FDelimiter write FDelimiter;
    property CommaText : string read GetCommaText write SetCommaText;
  end;

implementation

{ TJsStringListExt }

constructor TJsStringListExt.Create;
begin
  inherited Create;
  FDelimiter:=',';
end;

function TJsStringListExt.GetCommaText: string;
var
  S: string;
  P: PChar;
  I, Count: Integer;
begin
  Count := GetCount;
  if (Count = 1) and (Get(0) = '') then
    Result := '""'
  else
  begin
    Result := '';
    for I := 0 to Count - 1 do
    begin
      S := Get(I);
      P := PChar(S);
      while not (P^ in [#0..' ','"',FDelimiter]) do P := CharNext(P);
      if (P^ <> #0) then S := AnsiQuotedStr(S, '"');
      Result := Result + S + ',';
    end;
    System.Delete(Result, Length(Result), 1);
  end;
end;

procedure TJsStringListExt.SetCommaText(const Value: string);
var
  P, P1: PChar;
  S: string;
begin
  BeginUpdate;
  try
    Clear;
    P := PChar(Value);
    while P^ in [#1..' '] do P := CharNext(P);
    while P^ <> #0 do
    begin
      if P^ = '"then
        S := AnsiExtractQuotedStr(P, '"')
      else
      begin
        P1 := P;
        while (P^ > ' ') and (P^ <> FDelimiter) do P := CharNext(P);
        SetString(S, P1, P - P1);
      end;
      Add(S);
      while P^ in [#1..' '] do P := CharNext(P);
      if P^ = FDelimiter then
        repeat
          P := CharNext(P);
        until not (P^ in [#1..' ']);
    end;
  finally
    EndUpdate;
  end;
end;

end.
I come from outer space to save the human race
  Mit Zitat antworten Zitat