delphi 'string' 文字怎么会超过 255?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8767899/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 01:20:38  来源:igfitidea点击:

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

stringdelphisizedelphi-7

提问by PresleyDias

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

我正在研究delphi 7并且我正在研究字符串,我遇到了这个

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.

对于默认长度的字符串,即简单地声明为字符串,最大大小始终为 255。不允许 ShortString 增长到超过 255 个字符。

on delphi strings

德尔福字符串上

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

一旦我不得不在我的 delphi 代码中做这样的事情(那是一个非常大的查询)

  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.

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

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 myExtremlyLongStringis 8894characters.

如您所见,myExtremlyLongString的长度为8894 个字符。

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

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

EDITi used

编辑我用过

SetLength(sMyStringOF256characters,300);

but it doesnt work.

但它不起作用。

回答by Mikael Eriksson

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

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

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

您的答案在Long String (AnsiString)部分的文本中略有下降。

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

在当前版本的 Delphi 中,字符串类型只是 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.

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

sMyString:='ThisStringisofLength255'+'ThisStringisofLength255';

回答by Arjen van der Spek

Split it up into:

将其拆分为:

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

回答by paulsm4

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.

回到旧的 DOS/Turbo Pascal 时代,“字符串”确实被限制为 255 个字符。很大程度上是因为第一个字节包含字符串长度,一个字节只能有 0 到 255 之间的值。

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

这在当代版本的 Delphi 中不再是问题。

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

“ShortString”是旧的 DOS/Pascal 字符串类型的类型。

"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.

“LongString”长期以来一直是默认的字符串类型(包括我目前用于大多数生产工作的 Borland Delphi 2006)。LongStrings(又名“AnsiStrings”)保存 8 位字符,并且仅受可用内存的限制。

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.

Delphi 的最新版本(Delphi 2009 及更高版本,包括新的 Delphi XE2)现在都默认为多字节 Unicode“WideString”字符串。WideStrings 和 AnsiStrings 一样,在最大长度上也有效地“无限”。

This article explains in more detail:

这篇文章更详细地解释了:

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

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

回答by Nethy

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.

Delphi 中的字符串类型(与最多只能为 255 的短字符串不同)可以与您的内存一样大。

回答by Cameron M

You could try using the StringBuilder class:

您可以尝试使用 StringBuilder 类:

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;

回答by Dudeist

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.

如果您在 Delphi 中需要非常长的字符串,您可以从其他资源(如 txt 文件或带有任何扩展名的纯文本)加载它。我正在使用它并且它有效。您可以使用纯文本行号创建“类似”数组表。在 delphi 代码中,您可以按照@arjen van der Spek 和其他人所说的那样做。

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

对我来说,文本格式为 var 的文件 -

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

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.

缺点:文本代码是开放的,任何人都可以阅读它在记事本等中打开文件。