delphi'string'文字怎么可能超过255? [英] how can delphi 'string' literals be more than 255?

查看:1046
本文介绍了delphi'string'文字怎么可能超过255?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 delphi 7 ,我正在研究一个字符串,我遇到了这个


一个默认长度的字符串,即简单地表示为字符串,最大大小始终为255. ShortString不允许增长到超过255个字符。


delphi strings



  var 
sMyStringOF256characters:string;
ilength:integer;
begin
sMyStringOF256characters:='ThisStringisofLength256,ThisStringisofLength256,.....'
// sMyStringOF256字符的长度为256
end;

我得到这个错误


[错误] u_home.pas(38):字符串文字最多可能有255个元素。


但是当我尝试这个

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

,结果是





正如你可以看到 myExtremlyLongString 8894 字符。



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



编辑
i

  SetLength(sMyStringOF256characters,300); 

但它不工作。

解决方案


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


长字符串(AnsiString)中的文本中您的答案有所下降。


在当前版本的Delphi中,字符串类型只是
的一个别名AnsiString,


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

  sMyString:='ThisStringisofLength255'+'ThisStringisofLength255'; 


im working on delphi 7 and i was working on a strings, i came across this

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.

on delphi strings

once i had to do something like this in my delphi code (that was for a really big query)

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

i get this error

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

but when i try this

    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; 

and the result is

As you can see the length of myExtremlyLongString is 8894 characters.

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

EDIT i used

SetLength(sMyStringOF256characters,300);

but it doesnt work.

解决方案

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

You have your answer a bit down in the text in section Long String (AnsiString).

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

So string is not limited to 255 characters but a string literal is. That means that you can build a string that is longer than 255 characters but you can not have a string value in code that is longer than 255 characters. You need to split them if you want that.

sMyString:='ThisStringisofLength255'+'ThisStringisofLength255';

这篇关于delphi'string'文字怎么可能超过255?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆