How to fix compilation error: "String exceeds line"

How to fix compilation error: "String exceeds line"

我是 Delphi 编程新手。我想创建一个简单的字符串变量,其中包含一个新行,如下所示:

S := 'MAR 11, 2021  4:38 AM

Ciao';

这是短节目

program StrTest;

uses
  Classes, SysUtils;
var
  S : String;
begin
  S := 'MAR 11, 2021  4:38 AM

Ciao';
  writeln(S);
  Readln;

end.

但我收到此错误消息:

String exceeds line

即使我尝试最大化字符串变量的容量。看起来好像不能在其中换行,但我至少需要一个来进行测试。

你混淆了两个不相关的东西。源代码 中的字符串文字 中不允许换行符,这就是您收到错误的原因。 OTOH,它们 are 在字符串 values 中允许在 运行-time,所以你可以写

S := 'MAR 11, 2021  4:38 AM ' + #13#10 + 'Ciao';

或者,更简洁地说:

S := 'MAR 11, 2021  4:38 AM '#13#10'Ciao';

其中 #13 是“回车符 return”的 ASCII 代码,#10 是“换行符”的 ASCII 代码。请参阅此答案 ,以获得基于预定义 sLineBreak 常量的更好的跨平台解决方案。