且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将ShortInt数组转换为String,Delphi

更新时间:2023-01-16 09:48:59

我怀疑你的代码很慢,执行字符串的不必要的重新分配。然而,没有看到你的代码,很难确定。



可能最简单的方法来编码算法是使用 TStringBuilder 。无论是否提供足够的性能,只有你可以说。

  sb:= TStringBuilder.Create; 
try
for i:= 0 to high(buffer)do
begin
sb.Append(IntToStr(buffer [i]));
if i< high(buffer)then
sb.Append(',');
结束
str:= sb.ToString;
finally
sb.Free;
结束


I'm doing the way I learned, that is: with a FOR and taking the Index array one by one, but it is leaving too slow, would otherwise convert it to a String? that leaves quicker?

In my case it would be a Dynamic Array of ShortInt.

For example, given this input:

[0,20,-15]

I would like the following output:

0,20,-15

I suspect that your code is slow because it is performing unnecessary reallocations of the string. However, without seeing your code it's hard to be sure.

Probably the simplest way to code your algorithm is to use TStringBuilder. Whether or not that gives sufficient performance, only you can say.

sb := TStringBuilder.Create;
try
  for i := 0 to high(buffer) do
  begin
    sb.Append(IntToStr(buffer[i]));
    if i<high(buffer) then
      sb.Append(',');
  end;
  str := sb.ToString;
finally
  sb.Free;
end;