how can delphi 'string' literals be more than 255?

41,094

Solution 1

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

Solution 2

Split it up into:

sMyStringOF256characters := 
  'ThisStringis' +
  'ofLength256' +
  'And ManyManyManyManyManyManyManyManyManyManyManyManyMany' + 
  'ManyManyManyManyManyManyManyManyManyManyManyManyMany' + 
  'ManyManyManyManyManyManyManyManyManyManyManyManyMany' + 
  'ManyManyManyManyManyManyManyManyManyManyManyManyMany' + 
  'ManyManyManyManyManyManyManyManyManyManyManyManyMany' + 
  'ManyManyManyManyManyManyManyManyManyManyManyManyMany' + 
  'ManyManyManyManyManyManyManyManyManyManyManyManyMany' + 
  'ManyManyManyManyManyManyManyManyManyManyManyManyMany' + 
  'CharactersCharactersCharactersCharactersCharactersCharactersCharactersCharacters';

Solution 3

Back in old DOS/Turbo Pascal days, "strings" were indeed limited to 255 characters. In large part because the 1st byte contained the string length, and a byte can only have a value between 0 and 255.

That is no longer an issue in contemporary versions of Delphi.

"ShortString" is the type for the old DOS/Pascal string type.

"LongString" has been the default string type for a long time (including the Borland Delphi 2006 I currently use for most production work). LongStrings (aka "AnsiStrings") hold 8-bit characters, and are limited only by available memory.

Recent versions of Delphi (Delphi 2009 and higher, including the new Delphi XE2) all now default to multi-byte Unicode "WideString" strings. WideStrings, like AnsiStrings, are also effectively "unlimited" in maximum length.

This article explains in more detail:

http://delphi.about.com/od/beginners/l/aa071800a.htm

Solution 4

The difference is that in your first code example you are putting the string as part of your code - literal string. That has a limitation on how many characters it will allow.

In your second code example you are generating it dynamically and not putting it as one big literal string.

String type in Delphi (unlike shortstring that can only be up to 255) can be as big as your memory.

Solution 5

You could try using the StringBuilder class:

procedure TestStringBuilder;
var
    I: Integer;
    StringBuilder: TStringBuilder;
begin
    StringBuilder := TStringBuilder.Create;
    try
        for I := 1 to 10 do
        begin
            StringBuilder.Append('a string ');
            StringBuilder.Append(66); //add an integer
            StringBuilder.Append(sLineBreak); //add new line
        end;

        OutputWriteLine('Final string builder length: ' + IntToStr(StringBuilder.Length));
    finally
        StringBuilder.Free;
    end;
end;
Share:
41,094
PresleyDias
Author by

PresleyDias

By Day : MEAN Stack developer! By Night : Digging into Delphi, building Raspberry Pi stuff, working on WordPress website! If you want to know more about what I do, then check by blog Slaay

Updated on March 26, 2020

Comments

  • PresleyDias
    PresleyDias about 4 years

    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.