I just came across an answer on SO with a curious syntax:
How do I include a newline character in a string in Delphi?
MyString := 'Hello,' + ^M + ^J + 'world!';
I've been using Delphi for several years now, but I didn't know you could use the circumflex sign for control characters.
Is this just a left over from the early Delphi or Turbo Pascal days?
Should it be used nowadays?
PS: I'm not asking about advice on the line break characters, there is sLineBreak and other methods as discussed in the original question.
No, it is not from Turbo Pascal days. It is from decades before TP, and before MS-DOS, and probably even before UNIX. Something old like first 300 bit-per-second dialup modems and DEC VT-52 terminal, RT-8 OS on PDP-8 machine and early version of C. Or maybe even older - though everything older to me is mere legends :-).
"^" sign is shortcut for "Ctrl" key. So ^C
in traditional notation stands for Ctrl+C
in Microsoft notation. That notation was vastly used for textmode menus in MS-DOS times, like in the aforementioned Turbo Pascal, Norton Utilities, DOS Navigator, etc.
Out of my memory you can consider "^" for "subtract 64".
So as Chr(65)
is 'A'
then Chr(1)
would be ^A
.
And ^@
would be #0
:-) AFAIR in MS-DOS times pressing Ctrl+Shift+"2/@" would actually produce #0
into BIOS keyboard buffer :-)
^[ would AFAIR be #27
aka Esc(ape) char - and if you run telnet.exe
you would see it prompted as the escape character.
So Turbo Pascal long ago chosen to follow the time-blessed convention, and then rules of backward compatibility engaged ever since. Personally, i take 'bla-bla'^M^J'foo-baz'
literal more string-like than 'bla-bla'#13#10'foo-baz'
when you want it on one line. And constructing the value with plus is better fit when your literal takes several source lines.
The pity is that syntax highlighting in Delphi IDE is hopelessly broken on that kind of constants.
Yes this is a legacy from days of yore.
And no you should not get into the habit of using this feature. Remember that code is read more often than it is written. Always think of your readers who most likely won't know what that syntax means.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With