Delphi – how can delphi ‘string’ literals be more than 255

delphidelphi-7sizestring

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

enter image description here

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.

Best Answer

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';