本文介绍了将ShortInt数组转换为String,Delphi的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做我学到的方法,那就是:
和FOR一起接一个Index数组,但是它太慢了,否则会把它转换成一个String?这样会更快?



在我的情况下,它将是一个ShortInt的动态数组。



例如,给定这个输入:

  [0,20,-15] 

我想要以下输出:

  0,20, 15 


解决方案

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



可能最简单的方法来编码算法是使用 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;

这篇关于将ShortInt数组转换为String,Delphi的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 18:16