1

I am new to Delphi programming. I want to create a simple string variable where it contains a new line inside of it, like this:

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

Ciao';

This is the short program.

program StrTest;

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

Ciao';
  writeln(S);
  Readln;

end.

But I get this error message:

String exceeds line

Even if I try to maximize the capacity of the string variable. It looks as though it can't take a new line inside of it, but I need at least one for my test.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
OutOfMemoryError
  • 391
  • 2
  • 4
  • 16

1 Answers1

4

You are mixing up two unrelated things. Line breaks are not permitted within string literals in source code, and this is why you are getting the error. OTOH, they are permitted within string values at run-time, so you can write

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

Or, more concisely:

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

Where #13 is the ASCII code for a "carriage return" and #10 for a "line feed". See this answer https://stackoverflow.com/a/254997/2663863 for a better and cross-platform solution based on the predefined sLineBreak constant.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
MartynA
  • 30,454
  • 4
  • 32
  • 73
  • Two minor (possibly helpful) additions: (1) "string values in source code" are technically known as "string literals". (2) Personally, I'm might be a bit of a golfer, but I often use the possibility to write `'alpha' + #13#10 + 'beta'` more succinctly as `'alpha'#13#10'beta'`. – Andreas Rejbrand Mar 12 '21 at 14:10
  • Thanks for the suggestions @AndreasRejbrand; I've edited them into the answer. – MartynA Mar 12 '21 at 14:17
  • 1
    @ALombardo sLineBreak is already mentioned at the end of the answer – Delphi Coder Mar 12 '21 at 18:59