Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 302
PresleyDias Avatar asked Jan 07 '12 06:01

PresleyDias


People also ask

What are the two types of string literals?

A string literal with the prefix L is a wide string literal. A string literal without the prefix L is an ordinary or narrow string literal. The type of narrow string literal is array of char .

How long is a string Delphi?

The Delphi language supports short-string types - in effect, subtypes of ShortString - whose maximum length is anywhere from 0 to 255 characters.

What is difference between literal and string literal?

Definition. String literal in Java is a set of characters that is created by enclosing them inside a pair of double quotes. In contrast, String Object is a Java is a set of characters that is created using the new() operator. Thus, this explains the main difference between string literal and string object.

How many types of string literals are there?

There are three sets of literal types available in TypeScript today: strings, numbers, and booleans; by using literal types you can allow an exact value which a string, number, or boolean must have.


5 Answers

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';
like image 185
Mikael Eriksson Avatar answered Sep 22 '22 21:09

Mikael Eriksson


Split it up into:

sMyStringOF256characters := 
  'ThisStringis' +
  'ofLength256' +
  'And ManyManyManyManyManyManyManyManyManyManyManyManyMany' + 
  'ManyManyManyManyManyManyManyManyManyManyManyManyMany' + 
  'ManyManyManyManyManyManyManyManyManyManyManyManyMany' + 
  'ManyManyManyManyManyManyManyManyManyManyManyManyMany' + 
  'ManyManyManyManyManyManyManyManyManyManyManyManyMany' + 
  'ManyManyManyManyManyManyManyManyManyManyManyManyMany' + 
  'ManyManyManyManyManyManyManyManyManyManyManyManyMany' + 
  'ManyManyManyManyManyManyManyManyManyManyManyManyMany' + 
  'CharactersCharactersCharactersCharactersCharactersCharactersCharactersCharacters';
like image 37
Arjen van der Spek Avatar answered Sep 24 '22 21:09

Arjen van der Spek


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

like image 25
paulsm4 Avatar answered Sep 25 '22 21:09

paulsm4


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.

like image 37
Nethy Avatar answered Sep 23 '22 21:09

Nethy


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;
like image 24
Cameron M Avatar answered Sep 25 '22 21:09

Cameron M