如何将字符串元素数组连接成一个字符串

时间:2015-05-10 20:12:10

标签: arrays string delphi string-concatenation delphi-2007

如何将字符串数组转换为字符串(使用分隔符char)?我的意思是,是否有一些我可以使用的系统功能而不是编写我自己的功能?

4 个答案:

答案 0 :(得分:4)

由于您使用的是Delphi 2007,您必须自己动手:

function StrArrayJoin(const StringArray : array of string; const Separator : string) : string;
var
  i : Integer;
begin
  Result := '';
  for i := low(StringArray) to high(StringArray) do
    Result := Result + StringArray[i] + Separator;

  Delete(Result, Length(Result), 1);
end;

只需遍历阵列并与分离器连接即可。

一个小小的测试例子:

procedure TForm1.FormCreate(Sender: TObject);
begin
  Caption :=StrArrayJoin(['This', 'is', 'a', 'test'], ' ');
end;

答案 1 :(得分:4)

如果您使用的是更新版本的 Delphi,则可以使用 TStringHelper.Join,请参阅:http://docwiki.embarcadero.com/Libraries/Sydney/en/System.SysUtils.TStringHelper.Join

Writeln(String.Join(',', ['String1', 'String2', 'String3']));

输出将是:String1,String2,String3

答案 2 :(得分:3)

接受的答案在速度方面并不理想,尤其是在使用多个线程的情况下。这种方法在单核上快3倍,并且在SMP上可以很好地扩展。

function JoinStrings(const s: array of string; Delimiter: Char): string;
var
  i, c: Integer;
  p: PChar;
begin
  c := 0;
  for i := 0 to High(s) do
    Inc(c, Length(s[i]));
  SetLength(Result, c + High(s));
  p := PChar(Result);
  for i := 0 to High(s) do begin
    if i > 0 then begin
      p^ := Delimiter;
      Inc(p);
    end;
    Move(PChar(s[i])^, p^, SizeOf(Char)*Length(s[i]));
    Inc(p, Length(s[i]));
  end;
end;

速度测试:

program Project1;

{$APPTYPE CONSOLE}

uses
  Windows, SysUtils, StrUtils, Classes;

function StrArrayJoin(const StringArray: array of string; const Separator: string) : string;
var
  i : Integer;
begin
  Result := '';
  for i := low(StringArray) to high(StringArray) do
    Result := Result + StringArray[i] + Separator;
  Delete(Result, Length(Result), 1);
end;

function JoinStrings(const s: array of string; Delimiter: Char): string;
var
  i, c: Integer;
  p: PChar;
begin
  c := 0;
  for i := 0 to High(s) do
    Inc(c, Length(s[i]));
  SetLength(Result, c + High(s));
  p := PChar(Result);
  for i := 0 to High(s) do begin
    if i > 0 then begin
      p^ := Delimiter;
      Inc(p);
    end;
    Move(PChar(s[i])^, p^, SizeOf(Char)*Length(s[i]));
    Inc(p, Length(s[i]));
  end;
end;

var
  TestData: array of string;

type
  TTestThread = class(TThread)
  protected
    Func: Boolean;
    Count: Integer;
    procedure Execute; override;
  end;

procedure TTestThread.Execute;
var
  dtStart: TDateTime;
  i: Integer;
begin
  dtStart := Now;
  Count := 0;
  repeat
    for i := 1 to 1000 do
      if Func then
        JoinStrings(TestData, ';')
      else
        StrArrayJoin(TestData, ';');
    InterlockedIncrement(Count);
  until Now > dtStart + 1/86400;
end;

procedure TestSmp(CpuCount: Integer; Func: Boolean);
var
  i, c: Integer;
  Threads: array of TTestThread;
begin
  SetLength(Threads, CpuCount);
  for i := 0 to High(Threads) do begin
    Threads[i] := TTestThread.Create(true);
    Threads[i].Func := Func;
    Threads[i].Resume;
  end;
  c := 0;
  for i := 0 to High(Threads) do begin
    Threads[i].WaitFor;
    Inc(c, Threads[i].Count);
    Threads[i].Free;
  end;
  WriteLn(c);
end;

procedure Init();
var
  i: Integer;
begin
  SetLength(TestData, 1000);
  for i := 0 to High(TestData) do
    TestData[i] := DupeString('x', Random(32));
end;

begin
  try
    Init();
    Assert(StrArrayJoin(TestData, ';') = JoinStrings(TestData, ';'));
    TestSmp(1, false);
    TestSmp(1, true);
    TestSmp(4, false);
    TestSmp(4, true);
  except
    on E:Exception do
      Writeln(E.Classname, ': ', E.Message);
  end;
  Readln;
end.

结果:在四核CPU上,JoinStrings的速度提高了12倍。

StrArrayJoin 1 thread:  55
JoinStrings  1 thread:  184
StrArrayJoin 4 threads: 58
JoinStrings  4 threads: 713

答案 3 :(得分:2)

在delphi for .NET中,您可以使用框架Join函数,而如果您不想依赖.NET框架,那么您可以链接开源JCL库:看看图书馆的IJclStringList interface

JclStringList.Join(',');

否则,正如评论中建议的那样,您只需使用TStringList:

arrayList.Delimiter := ',';
arrayList.QuoteChar := '';
joinedArray := arrayList.DelimitedText;

请注意最新版本的Delphi XE (因为我记得XE3)有一个TStringHelper类,它将Join方法添加到String类

 class function Join(const Separator: string; const Values: 
IEnumerator<string>): string; overload; static;