µ¨ÆÄÀÌ7ÀÇ TStringList¿¡¼ commaText¸¦ »ç¿ëÇØ º¸¸é, ÀǵµÇÏÁö ¾Ê°Ô °ø¹éµµ ÄÞ¸¶Ã³·³ ±¸ºÐÀÚ·Î »ç¿ëµÇ´Â °ÍÀ» ¾Ë ¼ö ÀÖ½À´Ï´Ù.
»óÀ§ ¹öÀü¿¡¼´Â StrictDelimiter °¡ »ý°Ü¼ º°·Î ÇÊ¿ä¾ø°ÚÁö¸¸, ¾ÆÁ÷µµ ÇÏÀ§ ¹öÀüÀ¸·Î °³¹ßÇÏ°í ÀÖ´Â »ç¶÷¿¡°Ô´Â À¯¿ëÇÑ ±â´ÉÀÔ´Ï´Ù.
¼Ò½º ±¸±Û¸µÇØ º¸½Ã¸é commaText´Â ¿Ã¶ó¿Í Àִµ¥,
delimitedText ´Â ¾È º¸ÀÌ´õ¶ó°í¿ä.
±×·¡¼ ½ÃÇè»ï¿¡ ±¸±Û¸µÇؼ ±¸ÇÑ ¼Ò½º¸¦ Á¶±Ý º¯°æÇØ delimitedText µµ °ø¹éÀÌ ±¸ºÐÀÚ·Î µÇÁö ¾Ê°Ô Àß µË´Ï´Ù. µµ¿ò µÇ½Ã±æ...
»ç¿ë¹ý:
var
tcs: TCommaStrings;
begin
tsc:= TCommaStrings.Create;
tsc.commaText:= '1 2, 3 4';
-> °á°ú:
tsc[0] -> 1 2
tsc[1] -> 3 4
end;
===== ÀÌÇÏ ¿ÀºêÁ§Æ®ÀÔ´Ï´Ù ====
{ TCommaStrings }
TCommaStrings = class(TStringList)
private
function GetCommaText: string;
procedure SetCommaText(const Value: string);
function GetDelimitedText: string;
procedure SetDelimitedText(const Value: string);
public
property CommaText: string read GetCommaText write SetCommaText;
property DelimitedText: string read GetDelimitedText write SetDelimitedText;
end;
{ TCommaStrings }
function TCommaStrings.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..' ', '"', ',']) do
P := CharNext(P);
if (P^ <> #0) then
S := AnsiQuotedStr(S, '"');
Result := Result + S + ',';
end;
System.Delete(Result, Length(Result), 1);
end;
end;
procedure TCommaStrings.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^ <> ',') do
P := CharNext(P);
SetString(S, P1, P - P1);
end;
Add(S);
while P^ in [#1..' '] do
P := CharNext(P);
if P^ = ',' then
begin
repeat
P := CharNext(P);
until
not (P^ in [#1..' ']);
if P^ = #0 then
Add('') {Trailing commas ARE another field!}
end;
end;
finally
EndUpdate;
end;
end;
function TCommaStrings.GetDelimitedText: 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..' ', '"', Delimiter {','}]) do
P := CharNext(P);
if (P^ <> #0) then
S := AnsiQuotedStr(S, '"');
Result := Result + S + Delimiter {','};
end;
System.Delete(Result, Length(Result), 1);
end;
end;
procedure TCommaStrings.SetDelimitedText(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^ <> Delimiter{ ','}) do
P := CharNext(P);
SetString(S, P1, P - P1);
end;
Add(S);
while P^ in [#1..' '] do
P := CharNext(P);
if P^ = Delimiter{ ','} then
begin
repeat
P := CharNext(P);
until
not (P^ in [#1..' ']);
if P^ = #0 then
Add('') {Trailing commas ARE another field!}
end;
end;
finally
EndUpdate;
end;
end;