Substitute text into a string

General FreeBASIC programming questions.
Richard
Posts: 2958
Joined: Jan 15, 2007 20:44
Location: Australia

Substitute text into a string

Postby Richard » Mar 14, 2019 7:15

How can I put the body of a #define into a string so it can be printed ?
If I use #define identifier “body” the quotes get in the way of substitution elsewhere.

Code: Select all

#define identifier body
Dim As String txt
txt = " identifier "
Print txt  ' I want it to print 'body', not 'identifier'
MrSwiss
Posts: 3305
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Substitute text into a string

Postby MrSwiss » Mar 14, 2019 13:13

AFAIK, compiling with -pp tells the preprocessor to expand the #Define (#Macro)
into a string, which whould then be printable ... (no direct method I know of).
Foneo
Posts: 18
Joined: Sep 17, 2006 18:41
Location: Hampshire, UK

Re: Substitute text into a string

Postby Foneo » Mar 14, 2019 15:24

Since the macro processor can stringize parameters (and if you are prepared to have another macro), this works.

Code: Select all

#define stringize(param) #param
#define identifier body

Dim As String txt
txt = stringize(identifier)
Print stringize(identifier)  ' prints body
Print txt  ' prints body
Richard
Posts: 2958
Joined: Jan 15, 2007 20:44
Location: Australia

Re: Substitute text into a string

Postby Richard » Mar 14, 2019 22:58

Thanks @Foneo.
Your demonstration code solved the problem neatly.
Maryjohn0192
Posts: 1
Joined: Mar 21, 2019 10:03
Contact:

Re: Substitute text into a string

Postby Maryjohn0192 » Mar 21, 2019 10:05

thanks

Return to “General”

Who is online

Users browsing this forum: No registered users and 1 guest