Creating Text with Form, Mold, Join, etc.Author: Carl Sassenrath 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.
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.)
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).
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):
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.
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.
INSERT, APPEND, REPEND will also convert values to strings when the target series is a string. This can be handy:
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:
Which allows code like:
Summary of the standard string formatting functions:
|