Multi-line string literal

New to FreeBASIC? Post your questions here.
Post Reply
PeterHu
Posts: 159
Joined: Jul 24, 2022 4:57

Multi-line string literal

Post by PeterHu »

By searching in the manual,I failed to find how to define a multi-line string lieteral in a simple way( maybe something like raw string with multiline).So I am wondering is there any other options to write this `longstr` which will display exactly as `sub introduction()`.
Thanks in advance.

Code: Select all

dim as string longstr= _ 
    !"sub introduction()\n" & _
    !"  dim k as integer\n" & _
    !"  screen scrmode\n" & _
    !"  color frcolor,bgcolor\n"& _
    !"  cls\n"& _
    !"  locate 22,18\n"& _
    !"  print \"Loosely based on a C64 program \'Mind Boggle\'\"\n" & _
    !"  locate 24,10\n"& _
    !"  print \"    from COMPUTE!s Gzsette Disk May 1984\"\n"& _
    !"  print_colord(12,20,\" M I N D  B O G G L E\")\n"& _
"end sub "

print longstr

sub introduction()

    dim k as integer
    screen scrmode
    color frcolor,bgcolor
    cls
    locate 22,18
    print "Loosely based on a C64 program 'Mind Boggle'"
    locate 24,10
    print "    from COMPUTE!s Gzsette Disk May 1984"
    print_colord(12,20," M I N D  B O G G L E")
end sub 

Last edited by PeterHu on Mar 12, 2024 1:41, edited 3 times in total.
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Multi-line string literal

Post by dodicat »

You can use a raw string as a macro parameter.
You can end each line with | _
an then substitute | with chr(10).
I don't think that the pipe character (|) is used anywhere in fb syntax, so it will only exist where you put it and won't clash if substituted with chr(10)

Code: Select all



#define RAW(p...) #p

var s= RAW(_
  sub introduction()| _
  dim k as integer| _
  screen 19| _
  color frcolor,bgcolor| _
  cls| _
  locate 22,18| _
  print "Loosely based on a C64 program 'Mind Boggle'"| _
  locate 24,10| _
  print "    from COMPUTE!s Gzsette Disk May 1984"| _
  print_colord(12,20," M I N D  B O G G L E")| _
end sub)


for n as long=0 to len(s)-1
    if s[n]=asc("|") then s[n]=10
next n

print s


sleep 
 
Note:
EDITED:
Use the pipe (|) instead of a colon (:).
PeterHu
Posts: 159
Joined: Jul 24, 2022 4:57

Re: Multi-line string literal

Post by PeterHu »

This is Great!Thank you!
Post Reply