Delphi如何“字符串”?文字超过 255 个字符?

发布于 2024-12-25 22:23:28 字数 1461 浏览 0 评论 0原文

我正在使用 Delphi 7 和 Strings,我遇到了这个:

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

有一次我必须在我的 Delphi 代码中执行类似的操作(这是一个非常大的查询):

var
  sMyStringOF256characters : String;
  ilength : Integer;
begin
  sMyStringOF256characters := 'ThisStringisofLength256,ThisStringisofLength256,.....'
  // length of sMyStringOF256characters is 256
end;

...我收到此错误:

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

但是当我尝试这个时:

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; 

...结果是:

Delphi Form show both Label's value

正如你可以看到的长度myExtremlyLongString8894 个字符。为什么 Delphi 没有给出任何错误,指出 myExtremlyLongString 的长度超过 255?我也用过这个,但是不起作用:

SetLength(sMyStringOF256characters, 300);

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

空袭的梦i 2025-01-01 22:23:28

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

您可以在长字符串 (AnsiString) 部分的文本中找到答案。

在当前版本的 Delphi 中,字符串类型只是
Ansi字符串,

因此字符串不限于 255 个字符,但字符串文字可以。这意味着您可以构建长度超过 255 个字符的字符串,但代码中的字符串值不能超过 255 个字符。如果你想要的话,你需要将它们分开。

sMyString:='ThisStringisofLength255'+'ThisStringisofLength255';

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';
陪你搞怪i 2025-01-01 22:23:28

将其分为:

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

Split it up into:

sMyStringOF256characters := 
  'ThisStringis' +
  'ofLength256' +
  'And ManyManyManyManyManyManyManyManyManyManyManyManyMany' + 
  'ManyManyManyManyManyManyManyManyManyManyManyManyMany' + 
  'ManyManyManyManyManyManyManyManyManyManyManyManyMany' + 
  'ManyManyManyManyManyManyManyManyManyManyManyManyMany' + 
  'ManyManyManyManyManyManyManyManyManyManyManyManyMany' + 
  'ManyManyManyManyManyManyManyManyManyManyManyManyMany' + 
  'ManyManyManyManyManyManyManyManyManyManyManyManyMany' + 
  'ManyManyManyManyManyManyManyManyManyManyManyManyMany' + 
  'CharactersCharactersCharactersCharactersCharactersCharactersCharactersCharacters';
怼怹恏 2025-01-01 22:23:28

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

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

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

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

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

本文更详细地解释:

http://delphi.about.com/od/初学者/l/aa071800a.htm

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

旧梦荧光笔 2025-01-01 22:23:28

不同之处在于,在第一个代码示例中,您将字符串作为代码的一部分 - 文字字符串。这对允许的字符数有限制。

在第二个代码示例中,您动态生成它,而不是将其作为一个大的文字字符串。

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

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.

在巴黎塔顶看东京樱花 2025-01-01 22:23:28

您可以尝试使用 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;

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;
遗弃M 2025-01-01 22:23:28

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

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

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

不利于将来的编辑。

优点:您可以使用任何长文本。

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文