3

我有一个宽字符串说 StringABC 我有一个数组说 ArrayABC,这个数组的元素是

ArrayABC[0]='A';
ArrayABC[1]='B';
ArrayABC[2]='C';

现在我必须stringABCStringABC := ''A','B','C'';

我正在尝试这样做

StringABC := '';
for (i:=0 to 2) do
begin
StringABC := StringABC + ArrayABC[i] + ',';
if i = 2
stringABC := StringABC + ArrayABC[i];
end

我得到的结果是stringABC = 'A,B,C'但不是''A','B','C'' 我应该在我的代码中改进什么?

4

3 回答 3

4
StringABC := '';
for (i:=0 to 2) do
begin
StringABC := StringABC + '''' + ArrayABC[i] + ''',';
if i = 2
stringABC := StringABC + '''' + ArrayABC[i] + '''';
end

虽然我只是将所有 i 的逗号连接起来,然后最后删除最后一个逗号。

于 2013-01-10T14:06:06.017 回答
4
Const
  StringArray:Array[7..11] of String=('A','B','C','D','E');

Function GetQuotedStringArray(const a:Array of String) : String;

var
 i:Integer;

 Function GetComma:String;
  begin
    if i <> High(a) then 
     Result :=',' else Result :='';
  end;

begin
  for I := Low(a) to High(a) do 
   Result := Result + QuotedStr(a[i]) + GetComma;
end;

procedure TForm4.Button1Click(Sender: TObject);
begin
  Showmessage(GetQuotedStringArray(StringArray));
end;
于 2013-01-10T14:22:40.030 回答
2

另一种选择:

const
  StringArray:Array[7..11] of String=('A','B','C','D','E');

function GetQuotedStringArray(const a:Array of String): String;
var
  i: Integer;
  ComStrings: TStringList; 
begin
  ComStrings := TStringList.Create;
  try
    ComStrings.QuoteChar := '''';

    for i := low(a) to high(a) do
      ComStrings.Add(a[i]);

    Result := ComStrings.CommaText;
  finally
    ComStrings.Free;
  end;
end;
于 2013-01-10T18:53:16.660 回答