Example

Creating Text with Form, Mold, Join, etc.

Author: Carl Sassenrath
Return to REBOL Cookbook

REBOL offers many ways to convert a value to a string. This example was written to help REBOL beginners understand the use of string creation functions like FORM, REFORM, JOIN, REJOIN, MOLD, REDUCE, etc.

Note: in the examples below, what follows each example is what you would see if you displayed the STR result on the console. A PRINT is implied for each example (but we did not want to show it in order to keep the examples less confusing.)

FORM converts a value to a string. It does not evaluate blocks, but put a space between each value of a block.


    str: form 123
    123
    str: form "read"
    read
    str: form [123 + 321]
    123 + 321
    str: form [1.2.3 "read" now]
    1.2.3 read now

REFORM is the same as FORM but REDUCEs a block to its final values first. This function is very useful for generating human readable strings. (REFORM does the same thing as PRINT but to a string rather than the console.)


    str: reform 123
    123
    str: reform [123 + 321]
    444
    str: reform [123 "read" now]
    123 read 31-Aug-2003/14:21:50-7:00

MOLD converts a value to a REBOL string that can be LOADed back into REBOL. It is normally used for saving data to files or sending data over a network (to be loaded again as values into REBOL). Note that blocks are not REDUCED (see REMOLD for that).


    str: mold 123
    123
    str: mold "read"
    "read"
    str: mold %file.txt
    %file.txt
    str: mold [join %dir/ %file.txt "text" now]
    [join %dir/ %file.txt "text" now]

The functions below are also available but are used less often:

REMOLD is the same as MOLD but does a REDUCE first (like REFORM does with FORM):


    str: remold [123 + 321]
    [444]
    str: remold [join %dir/ %file.txt "text" now]
    [%dir/file.txt "text" 31-Aug-2003/14:57:59-7:00]

REJOIN is similar to REFORM on a block but does not insert spaces. In addition, if the first value is a string series, its datatype is used for the resulting string. This is good for creating filenames, URLs, HTML tags, etc.


    str: rejoin [123 "read" now]
    123read31-Aug-2003/14:21:50-7:00
    str: rejoin [%dir/ "read" 123 ".txt"]
    %dir/read123.txt
    str: rejoin [http://www ".rebol.com/" 123 %.html]
    http://www.rebol.com/123.html
    str: rejoin [<FONT> " SIZE=" 4]
    <FONT SIZE=4>

JOIN is identical to REJOIN but takes two arguments. It was created to provide a join function similar to that found in other languages. The resulting datatype will be the same as the datatype of the first argument (if it's a string series). JOIN can also take a block (reduced) as the second argument.


    str: join %dir/ "read.txt"
    %dir/read.txt
    str: join http://www ".rebol.com"
    http://www.rebol.com
    str: join <FONT> " SIZE=4"
    <FONT SIZE=4>
    str: join http://www.rebol.com/ ["read" 123 ".txt"]
    http://www.rebol.com/read123.txt

INSERT, APPEND, REPEND will also convert values to strings when the target series is a string. This can be handy:


    str: "read"
    insert str 123
    123read
    str: "read"
    append str 123
    read123
    str: "read"
    repend str [123 + 321 ".txt"]
    read444.txt

REPEND means APPEND a REDUCED block. This last example is noteworthy, because it provides one of the most useful methods of string concatenation. For example it is often used for generating HTML output:


    html: make string! 10000
    emit: func [data] [repend html data]

Which allows code like:


    emit [
        <HTML><BODY>
        "The date is: " now/date
        </BODY></HTML>
    ]
    <HTML><BODY>The date is: 31-Aug-2003</BODY></HTML>

Summary of the standard string formatting functions:

Input Value

FORM

REFORM

MOLD

REMOLD

REJOIN

123

123

123

123

123

123

"test"

test

test

"test"

"test"

test

%file.txt

file.txt

file.txt

%file.txt

%file.txt

%file.txt

[1 + 2]

1 + 2

3

[1 + 2]

[3]

3

[%dir/ "www" 1 + 2]

dir/ www 1 + 2

dir/ www 3

[%dir/ "www" 1 + 2]

[%dir/ "www" 3]

%dir/www3

What to Remember?

If you're only going to remember a few of these functions, here are the ones to remember:

  • REFORM creates a human readable string.
  • MOLD creates a REBOL readable string.
  • REJOIN is great for making filenames and URLs.


2006 REBOL Technologies REBOL.com REBOL.net