How can Delphi 'string' literals have more than 255 characters?
Asked Answered
F

6

33

I'm working with Delphi 7 and Strings, and 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.

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 := '';
  for iCounter := 0 to 2500 do
  begin
    myExtremlyLongString := myExtremlyLongString + IntToStr(iCounter);
  end;
  Label1.Caption := myExtremlyLongString;
  Label2.Caption := IntToStr(Length(myExtremlyLongString));
end; 

...the result is:

Delphi Form show both Label's values

As you can see the length of myExtremlyLongString is 8894 characters. Why did Delphi not give any error saying the length is beyond 255 for myExtremlyLongString? I also used this but it doesn't work:

SetLength(sMyStringOF256characters, 300);
Fore answered 7/1, 2012 at 6:33 Comment(4)
Perhaps you are just reading wrong docs. BTW, its really unclear what do you mean by "dynamically assigned" as opposed to ... what?Pride
@user539484 : meaning during run time during a loop may be strVal:=mystring, like a loop value your assigning to the string variableFore
ah, scratch "dynamically" then, its about assignment vs. declaration. What you are quoting above is about declarations, more specifically - about compiler limitation on character literals. It has been connected to Turbo Pascal strings by historical means only.Pride
I guess they do not remove this limitation only because there is no practical reason to have string literal longer that 70 characters... Internally (for compiler) such literal is equivalent to array [0..N] of Char. Hope its clearer now.Pride
A
47

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';
Assiduity answered 7/1, 2012 at 7:14 Comment(2)
Hello from 2019, I'm very sorry to tell you this is still actual :-( But thanks for the answer!Casmey
2023 and still sameOverbear
P
12

Split it up into:

sMyStringOF256characters := 
  'ThisStringis' +
  'ofLength256' +
  'And ManyManyManyManyManyManyManyManyManyManyManyManyMany' + 
  'ManyManyManyManyManyManyManyManyManyManyManyManyMany' + 
  'ManyManyManyManyManyManyManyManyManyManyManyManyMany' + 
  'ManyManyManyManyManyManyManyManyManyManyManyManyMany' + 
  'ManyManyManyManyManyManyManyManyManyManyManyManyMany' + 
  'ManyManyManyManyManyManyManyManyManyManyManyManyMany' + 
  'ManyManyManyManyManyManyManyManyManyManyManyManyMany' + 
  'ManyManyManyManyManyManyManyManyManyManyManyManyMany' + 
  'CharactersCharactersCharactersCharactersCharactersCharactersCharactersCharacters';
Petrel answered 7/1, 2012 at 7:14 Comment(1)
+1, i had tried similar (less tha 255 for sure), for a query once, that time i thought, the limit is 255,so did not go beyond 255..nice example thanxFore
T
5

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

Transcendental answered 7/1, 2012 at 7:18 Comment(3)
This is no longer an issue in contemporary versions of Delphi, except in two places; String literals, and in subscripted strings like string[50], where (obviously!) the limit is 250 or less, in the case I mention, the limit is 50.Angleworm
@Warren P - you're absolutely correct. The workaround for string literals is to use the "'mystring' + " syntax discussed in other threads.Transcendental
This is incorrect information; most Delphi string types are limited to 2GB in size. :-( docwiki.embarcadero.com/RADStudio/XE3/en/String_TypesStraightforward
D
2

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.

Decorum answered 7/1, 2012 at 7:14 Comment(2)
+1, "can be as big as your memory. " thats a very big string thenFore
It cannot be as big as your memory. The biggest Delphi string types are still limited to 2GB of characters. docwiki.embarcadero.com/RADStudio/XE3/en/String_TypesStraightforward
K
1

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;
Kinsfolk answered 25/3, 2013 at 21:28 Comment(0)
M
0

If you need realy long string in Delphi, you can load it from other resources like a txt files or just plain text with any extension. Im using it and it works. You can create "like a" array tables using plain text lines numbers. In delphi code, you can do as @arjen van der Spek and others says only.

For me, files with text as var's formated -

sometext:string=
'txt...'+
'txt...'+
'txt...';

are bad for future editing.

pros: you can use any long text.

cons: text code is open, anybody can read it opening file in notepad etc.

Mona answered 18/1, 2013 at 22:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.