Delphi:记录中的字符串大于255个字符

Acr*_*ron 2 delphi string record

有没有办法在大于255个字符的记录中获取字符串?

编辑:

我有以下内容:

TQuery = Record
  Action: string[255];
  Data: string;
end;
Run Code Online (Sandbox Code Playgroud)

如果我现在说:

Test: TQuery;
Test.Data := 'ABCDEFGHIJKLMN...up to 255...AISDJIOAS'; //Shall be 255 chars
Run Code Online (Sandbox Code Playgroud)

它不起作用,编译器抱怨......如何解决?

Wou*_*ick 7

如果您希望能够将记录写入文件,则可以将字符串定义为ansichar数组.之后您可以将其视为字符串.

例:

program StrInRecTest;
{$APPTYPE CONSOLE}
uses SysUtils;

type
  TStringRec=
    packed record
        S:array[0..1023] of ansichar;
    end;

var
  StringRec:TStringRec;
  F:File of TStringRec;
begin
  StringRec.S := 'Hello';
  WriteLn(StringRec.S);
  WriteLn('First char is:'+StringRec.S[0]); // watch out with this


  // now let's try saving this to a file and reading it back...

  // make a long string with x-es
  FillChar(StringRec.S,Length(StringRec.S),'X');
  StringRec.S[High(StringRec.S)] := #0; // terminate the string

  WriteLn(StringRec.S);

  // write to a file
  AssignFile(F,'tmp.txt');
  ReWrite(F);
  Write(F,StringRec);
  CloseFile(F);

  WriteLn;

  // read from file
  AssignFile(F,'tmp.txt');
  Reset(F);
  Read(F,StringRec);
  CloseFile(F);

  WriteLn(StringRec.S); // yay, we've got our long string back

  ReadLn;
end.
Run Code Online (Sandbox Code Playgroud)


Too*_*the 5

Delphi和三个字符串

曾几何时,在pascal的早期,那里有短弦.它们由一个字节块组成,最大大小为256.第一个字节是长度字节:

5, H, e, l, l, o
Run Code Online (Sandbox Code Playgroud)

您可以定义固定长度的字符串以节省内存:

a: string[5];
Run Code Online (Sandbox Code Playgroud)

Windows使用C字符串,它是指向以0字符终止的内存块的指针.这些字符串不限于255个字节.首先,它们作为PChar(指向char的指针)提供.但后来默认字符串被解释为C类型字符串.你仍然可以使用短串:

a: string[22];
b: ShortString;
c: string; // C (Delphi) string
Run Code Online (Sandbox Code Playgroud)

使用Delphi 2009,引入了Unicode.现在每个字符串都是一个Unicode字符串.这是指向包含unicode字符的内存的指针.我们仍然有ShortString类型.AnsiString或PAnsiChar可以访问旧的ansi字符串.

现在字符串是指针,大小没有限制.但字符串文字仍然限制为255个字符.

  • 好故事!你注意到OP专门询问了"记录"吗?你没有用那个词!:-) (6认同)