Delphi 11 定义字符串类型 () vs [ ]

huangapple go评论62阅读模式
英文:

Delphi 11 Define String Type ( ) vs [ ]

问题

以下是您要的翻译:

我正在使用Delphi 11构建TurboPower BTree Filer,该工具应该支持Delphi直到2006版本。由于使用了更新的版本,我不得不将Char转换为AnsiChar,将String转换为AnsiString,但是在Delphi 11中,这种字符串类型定义失败了:

const
  IsamFileNameLen = 64;

type
  IsamFileName = ansistring[IsamFileNameLen];

它不喜欢方括号,所以我猜它想要括号(),但它也不喜欢那些?

问题是什么?

编辑:如果只是“string”,那就可以,但这不是我需要的,我需要AnsiString?

谢谢!
英文:

I'm using Delphi 11 to build TurboPower BTree Filer, which is supposed to support Delphi up to 2006 version. Since using newer version I have to convert Char to AnsiChar and String to AnsiString but this string type definition fails in Delphi 11:

const
  IsamFileNameLen = 64;

type
  IsamFileName = ansistring[IsamFileNameLen];

It doesn't like the square brackets, so I presume it wants parentheses (), but it didn't like those either?

What is the problem?

Edit: If it's just "string" then it is okay but it's not what I need, I need ansistring ?

Thanks!

答案1

得分: 3

确实,`string` 通常在 D2009 之前指的是 `AnsiString`,而在 D2009 之后指的是 `UnicodeString`。但是,只有当它用作**动态长字符串**时才如此。当它用作**固定长度短字符串**时,`string` 仍然指的是 ANSI 类型。因此,在所有版本中,正确的声明仍然相同:

const
IsamFileNameLen = 64;

type
IsamFileName = string[IsamFileNameLen];


这在 Delphi 的文档中有描述:

https://docwiki.embarcadero.com/RADStudio/en/String_Types_(Delphi)

> Delphi 语言支持短字符串类型 - 实际上是 [`ShortString`][1] 的子类型 - 其最大长度可以从 0 到 255 个字符不等。这些是由在保留字 [`string`][2] 后附加的方括号内的数字表示的。例如:
>
> ```
> var MyString: string[100];
> ```
>
> 创建了一个名为 `MyString` 的变量,其最大长度为 100 个字符。这等效于以下声明:
>
> ```
> type CString = string[100];
> var MyString: CString;
> ```

  [1]: http://docwiki.embarcadero.com/Libraries/en/System.ShortString
  [2]: http://docwiki.embarcadero.com/Libraries/en/System.String
英文:

It is true that string typically refers to AnsiString
before D2009, and to UnicodeString since D2009. But, only when used as a dynamic long string . When used as a fixed-length short string instead, string still refers to an ANSI type. So, the correct declaration remains the same in all versions:

const
  IsamFileNameLen = 64;

type
  IsamFileName = string[IsamFileNameLen];

This is described in Delphi's documentation:

https://docwiki.embarcadero.com/RADStudio/en/String_Types_(Delphi)

> The Delphi language supports short-string types - in effect, subtypes of ShortString - whose maximum length is anywhere from 0 to 255 characters. These are denoted by a bracketed numeral appended to the reserved word string. For example:
>
>
>
> var MyString: string[100];
>

>
> creates a variable called MyString, whose maximum length is 100 characters. This is equivalent to the declarations:
>
>
> type CString = string[100];
> var MyString: CString;
>

huangapple
  • 本文由 发表于 2023年2月10日 12:04:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/75406828.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定