What you are asking for requires the StrictDelimiter
property, which did not exist yet in Delphi 7. Without the StrictDelimiter
property, the DelimitedText
property setter includes spaces as delimiters regardless of the Delimiter
property, and you cannot change that behavior. So, you will have to parse the input string manually instead.
The following code is derived from the StrictDelimiter
logic used in later Delphi versions, and works fine in Delphi 7:
uses
SysUtils, Classes, Windows;
procedure SplitDelimitedText(const Value: string; List: TStrings; StrictDelimiter: Boolean = True);
var
P, P1: PChar;
S: string;
begin
if not StrictDelimiter then
begin
List.DelimitedText := Value;
Exit;
end;
List.BeginUpdate;
try
List.Clear;
P := PChar(Value);
while P^ <> #0 do
begin
if P^ = List.QuoteChar then
S := AnsiExtractQuotedStr(P, List.QuoteChar)
else
begin
P1 := P;
while (P^ <> #0) and (P^ <> List.Delimiter) do
P := CharNext(P);
SetString(S, P1, P - P1);
end;
List.Add(S);
if P^ = List.Delimiter then
begin
P1 := P;
if CharNext(P1)^ = #0 then
List.Add('');
P := CharNext(P);
end;
end;
finally
List.EndUpdate;
end;
end;
var
s: String;
sl1, sl2: TStringList;
i: Integer;
begin;
s := ' 0.985,EError can''t find,E| 0,5186,Name,6946';
sl1 := TStringList.Create;
try
sl1.Delimiter := '|';
sl1.QuoteChar := #0;
SplitDelimitedText(s, sl1);
sl2 := TStringList.Create;
try
sl2.Delimiter := ',';
sl2.QuoteChar := #0;
for I := 0 to sl1.Count-1 do
begin
SplitDelimitedText(sl1[i], sl2);
// use sl2 as needed...
end;
finally
sl2.Free;
end;
finally
sl1.Free;
end;
end;