加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

delphi’string’文字怎么可能超过255?

发布时间:2020-12-15 10:16:50 所属栏目:大数据 来源:网络整理
导读:我在delphi 7工作,我正在工作的字符串,我遇到这个 For a string of default length,that is,declared simply as string,max size is always 255. A ShortString is never allowed to grow to more than 255 characters. 在delphi strings 一旦我不得不在我
我在delphi 7工作,我正在工作的字符串,我遇到这个

For a string of default length,that is,declared simply as string,max size is always 255. A ShortString is never allowed to grow to more than 255 characters.

在delphi strings

一旦我不得不在我的delphi代码中做这样的事情(这是一个很大的查询)

var
    sMyStringOF256characters : string;
    ilength : integer;
    begin
       sMyStringOF256characters:='ThisStringisofLength256,ThisStringisofLength256,.....'
       //length of sMyStringOF256characters is 256
    end;

我得到这个错误

[Error] u_home.pas(38): String literals may have at most 255 elements.

但是当我尝试这个

var
      iCounter              : integer;
      myExtremlyLongString  : string;
   begin
      myExtremlyLongString:='';
      Label1.Caption:='';
      for iCounter:=0 to 2500 do
         begin
            myExtremlyLongString:=myExtremlyLongString+inttostr(iCounter);
            Label1.Caption:=myExtremlyLongString;
         end;
         Label2.Caption:=inttostr(length(myExtremlyLongString));
   end;

结果是

你可以看到myExtremlyLongString的长度是8894个字符。

为什么delphi没有给出任何错误,说myExtremlyLongString的长度超过255?

编辑
我用了

SetLength(sMyStringOF256characters,300);

但它没有工作。

解决方法

why did not delphi give any error saying the length is beyond 255 for
myExtremlyLongString?

在Long String(AnsiString)部分的文本中你有一个答案。

In current versions of Delphi,the string type is simply an alias for
AnsiString,

所以字符串不限于255个字符,而是一个字符串文字。这意味着您可以构建长度超过255个字符的字符串,但不能在长度超过255个字符的代码中使用字符串值。如果你愿意,你需要拆分它们。

sMyString:='ThisStringisofLength255'+'ThisStringisofLength255';

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读