Recent

Author Topic: In pascal multiple line string possible  (Read 3963 times)

Packs

  • Sr. Member
  • ****
  • Posts: 410
In pascal multiple line string possible
« on: November 28, 2024, 06:31:44 am »
In pascal multiple line string.

Handoko

  • Hero Member
  • *****
  • Posts: 5386
  • My goal: build my own game engine using Lazarus
Re: In pascal multiple line string possible
« Reply #1 on: November 28, 2024, 06:55:12 am »
Did you mean this:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   S: string;
  4. begin
  5.   S := 'This is a'            + LineEnding +
  6.        'multiple line string' + LineEnding +
  7.        'in Pascal.';
  8.   ShowMessage(S);
  9. end;

Packs

  • Sr. Member
  • ****
  • Posts: 410
Re: In pascal multiple line string possible
« Reply #2 on: November 28, 2024, 07:03:06 am »
Ok.

Thank you 🙏

Warfley

  • Hero Member
  • *****
  • Posts: 1856
Re: In pascal multiple line string possible
« Reply #3 on: November 28, 2024, 11:10:50 am »
If I have to write a lot of lines I personally use a macro:
Code: Pascal  [Select][+][-]
  1. {$Macro On}
  2. {$Define ln:=+LineEnding+}
  3.  
  4. const
  5.   MyString =
  6.     'Line1'ln
  7.     'Line2'ln
  8.     'Line3'ln
  9.     'Line3';

Handoko

  • Hero Member
  • *****
  • Posts: 5386
  • My goal: build my own game engine using Lazarus
Re: In pascal multiple line string possible
« Reply #4 on: November 28, 2024, 11:14:52 am »
Nice trick!

Packs

  • Sr. Member
  • ****
  • Posts: 410
Re: In pascal multiple line string possible
« Reply #5 on: November 28, 2024, 11:35:40 am »
Thank you 🙏

Thaddy

  • Hero Member
  • *****
  • Posts: 16419
  • Censorship about opinions does not belong here.
Re: In pascal multiple line string possible
« Reply #6 on: November 28, 2024, 12:01:15 pm »
I would not use that macro with that name, since ln is also a system function.
Macro fine, name not. Try al or something.
« Last Edit: November 28, 2024, 12:03:50 pm by Thaddy »
There is nothing wrong with being blunt. At a minimum it is also honest.

Warfley

  • Hero Member
  • *****
  • Posts: 1856
Re: In pascal multiple line string possible
« Reply #7 on: November 28, 2024, 12:45:31 pm »
Usually when I'm at the point where I need extended string constants in my code, I often put them in their own unit, with no code, so the macros don't really matter at all.

But yeah, generally short macros can be dangerous as its likely they collide with other existing names, an alternative is to undef the macro after use

Thaddy

  • Hero Member
  • *****
  • Posts: 16419
  • Censorship about opinions does not belong here.
Re: In pascal multiple line string possible
« Reply #8 on: November 28, 2024, 12:52:10 pm »
Usually I use an include. Similar.
« Last Edit: November 28, 2024, 01:14:13 pm by Thaddy »
There is nothing wrong with being blunt. At a minimum it is also honest.

LV

  • Full Member
  • ***
  • Posts: 197
Re: In pascal multiple line string possible
« Reply #9 on: November 28, 2024, 01:06:22 pm »
#13#10(win) = #10(Linux) = #13(MacOS) = sLineBreak

https://forum.lazarus.freepascal.org/index.php?topic=57659.0

syswinh.inc  ;)

Code: Pascal  [Select][+][-]
  1. const
  2.  LineEnding = #13#10;
  3. ...
  4. sLineBreak = LineEnding;
  5.  
« Last Edit: November 28, 2024, 01:29:20 pm by LV »

jamie

  • Hero Member
  • *****
  • Posts: 6791
Re: In pascal multiple line string possible
« Reply #10 on: November 28, 2024, 01:15:30 pm »
If I have to write a lot of lines I personally use a macro:
Code: Pascal  [Select][+][-]
  1. {$Macro On}
  2. {$Define ln:=+LineEnding+}
  3.  
  4. const
  5.   MyString =
  6.     'Line1'ln
  7.     'Line2'ln
  8.     'Line3'ln
  9.     'Line3';

Hmm, "ln" = natural logarithm

Jamie
The only true wisdom is knowing you know nothing

Thaddy

  • Hero Member
  • *****
  • Posts: 16419
  • Censorship about opinions does not belong here.
Re: In pascal multiple line string possible
« Reply #11 on: November 28, 2024, 01:46:06 pm »
Warfley already answered that and added an alternative.
There is nothing wrong with being blunt. At a minimum it is also honest.

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1456
    • Lebeau Software
Re: In pascal multiple line string possible
« Reply #12 on: November 28, 2024, 06:47:08 pm »
Delphi 12.0 introduced Multi-line String Literals, for example:

Code: Pascal  [Select][+][-]
  1. const
  2.   MyString = '''
  3.    Line1
  4.    Line2
  5.    Line3
  6.    Line4
  7.  ''';

Maybe FreePascal will add something like this someday.
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

Thaddy

  • Hero Member
  • *****
  • Posts: 16419
  • Censorship about opinions does not belong here.
Re: In pascal multiple line string possible
« Reply #13 on: November 28, 2024, 09:20:15 pm »
There is actually a patch for that, that I tested and confirmed to work, but it used back-ticks..
It is horrible, though. Refused for all the right reasons. But the code is available and working (syntax being not relevant to implementation)
The Delphi case should be refused too...
I have inline var syndrome, mind... Silly idiots.
« Last Edit: November 28, 2024, 09:28:09 pm by Thaddy »
There is nothing wrong with being blunt. At a minimum it is also honest.

MarkMLl

  • Hero Member
  • *****
  • Posts: 8136
Re: In pascal multiple line string possible
« Reply #14 on: November 28, 2024, 09:57:15 pm »
There is actually a patch for that, that I tested and confirmed to work, but it used back-ticks..

Which visually is probably not a bad choice: sequences of '""' etc. "get old fast".

I know that the core team is inclined to tell us to put multilines into resource files, but I maintain- impenitently and from experience- that they are wrong. If you're writing Pascal code that relies on- for example- an SQL fragment, the place to put that fragment is adjacent to the associated Pascal: and that's also the place to put the comments explaining what the combination is doing.

Unless, of course, somebody would like to argue that comments should be moved out of the unit and into a file of their own?

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

 

TinyPortal © 2005-2018