NewDocs_Strings_Two
Cooked String
A cooked string literal "is written between quotation mark " delimiters, has most characters are entered as glyphs, while a few must be cooked using escaped notation before being entered."
Escaped notation requires "a backslash \ followed by one character; the two symbols together are interpreted as one character (one code point)."
For example. Using escaped notation it is possible to enter the quotation mark as \" into a string delimited by quotation marks.
For example. In a cooked string a newline character, value 10, has to be cooked using escaped notation and written as \n. In a cooked string a newline character does not form a new line; a new line is visible only after the string is output by a procedure like puts.
A simple string:
sequence s = "Hello Euphoria!" --> Hello Euphoria!
A string with escaped characters:
sequence s = "\"Hello\" Euphoria!" --> "Hello" Euphoria!
Text entered as a cooked string:
- The characters are written enclosed by quotation mark " delimiters.
- You must write a literal quotation mark character " using escaped notation \" .
- Must be written on one line only.
- Requires escaped notation for a few specific characters.
- After encoding, you can not identify the string has having been entered in cooked notation.
Escaped Character |
Name | Integer |
---|---|---|
\n | newline | 10 |
\r | carriage-return | 13 |
\t | tab | 9 |
\\ | backslash | 92 |
\" | quotation mark | 34 |
\' | apostrophe | 39 |
\0 | null | 0 |
\e | escape | 27 |
\E | escape | 27 |
Raw String
With raw string assignment "each character is entered as its authentic self."
_ (.\ \\ \\_________ (\--/) ( ( . . ) / / '-.O.' _\ / _/ _\
For example. In a raw string pressing the enter key results in an actual newline being created--the character is added raw.
Raw string literals are enclosed by triple quotation mark """ or grave accent ` delimiters:
sequence rs = """ Hello! \ Euphoria! """
Because of raw notation the newline (10), tab (9), and backslash (92) characters are input into the string in their raw form; that is why you see the text formatted just as you entered the text. Displaying the value of rs using pretty:pretty_print:
{72'H',101'e',108'l',108'l',111'o',33'!',10,32' ',32' ',32' ',32' ',32' ', 32' ',92'\',10,9,69'E',117'u',112'p',104'h',111'o',114'r',105'i',97'a',33'!'}
Hint: A programmer's editor can be configured to be key specific: a Tab key may enter several spaces 32 instead of one tab 10 character, and on a windows system an Enter key may just enter a newline 10 instead instead of {13, 10}.
Text entered as a raw string:
- Choose from either triple quotation mark """ or single grave-accent ` delimiters. The string must be enclosed with the same beginning and ending delimiters.
- The chosen delimiter can not appear inside a raw string.
- You can not enter a character using escaped notation--each character is "raw."
- Strings can be written on one line or across several lines.
- All carriage-return (13) characters are stripped from the string.
- After encoding, you can not identify the string has having been entered in raw notation.
You may find raw string notation convenient for:
- Entering text that contains quotation mark characters as part of the string. Raw notation saves on using escaped characters.
- Entering text for a regular expression. Raw notation saves on entering escaped characters that in turn have to be escaped if entered using cooked notation.
- Entering text for a help screen.
- Creating ASCII art.
Hint: If you are creating a string where the syntax for entering a string conflicts with the values you want entered you can always concatenate small, easy to create, strings into one long string:
object TQ , GAQ , QQ TQ = `This text contains """ for some reason .` GAQ = """This text contains a grave-accent ` for some reason.""" QQ = """This text contains a grave-accent `""" & ` and """ for some reason.`
Text Output
You must remember that Euphoria does not have a character or string data-type. When you assign a literal text value to a variable the character glyphs are automatically encoded as numbers. Effectively the glyphs of text data are discarded after encoding.
You can output the value of a variable or output the value of string literal as if Euphoria did have a string data-type:
puts
The puts procedure displays the text of a character string sequence:
sequence str = "Hello Euphoria!" puts(1, str ) --> Hello Euphoria!
The puts procedure only works with a string which is defined as a flat sequence of encoded character values. It will not work with a nested sequence:
puts(1, { "not", "a", "string" } ) --> error -- sequence found inside character string
print, ?
The print ( or ? the shortcut for print ) will display the encoded character values as numbers:
print(1, "Hello Euphoria!" ) --> {72,101,108,108,111,32,69,117,112,104,111,114,105,97,33}
pretty_print
The standard library pretty:pretty_print procedure shows you the details of text data.
include std/pretty.e pretty:pretty_print(1, "Hello Euphoria!" ) --> {72'H',101'e',108'l',108'l',111'o',32' ',69'E',117'u',112'p',104'h',111'o', --> 114'r',105'i',97'a',33'!'} -- same output as: pretty_print(1, "Hello Euphoria!", {1} )
The the encoded ASCII value and its grapheme are displayed as pairs.
If the third argument is {2} then just text is displayed:
pretty:pretty_print(1, "Hello Euphoria!", {2} ) --> Hello Euphoria!
If the third argument is {3} then special characters are shown in escaped format:
pretty:pretty_print(1, `"Hello \ " Euphoria!` ) --> "\"Hello \\ \" Euphoria!"
display
The std/console.e standard library contains the display procedure. The display procedure has a heuristic algorithm that tries to guess if data is numeric or text based. You can use it to output text data that fails if you use the puts procedure:
include std/console.e console:display( "This","is","not","a","string."} ) --> -- { -- "This", -- "is", -- "not", -- "a", -- "string." -- }
Sometimes the display procedure can not guess if a small integer value is intended to be a number or an encoded character value:
display( "!" ) --> ! display( '!' ) --> 33 display( 33 ) --> 33
printf
Use the printf procedure with the %s format specifier for text output.
Each format specifier, like %s, will output the value of one item. The trick to using printf is recognizing how many items there are in the third argument.
Argument | Items Count |
Specifiers Count |
Example |
---|---|---|---|
"Hello" {'H','e','l','l','o'} |
5 | 5 |
printf(1, "%s%s%s%s%s", "Hello" ) --> Hello |
{"Hello"} | 1 | 1 |
printf(1, "%s", {"Hello"} ) --> Hello |
You must supply at least one item for each format specifier. If you do not have enough items in the third argument then the procedure fails with an error message. But, if you have too many items in the third argument then the surplus is quietly ignored.
printf(1, "%s", "Hello Euphoria!" ) --> H printf(1, "%s", { "Hello Euphoria!" } ) --> Hello Euphoria
When using Euphoria you must think in terms of an atom as an individual item and sequence as a collective item.
sprintf
The sprintf function is analogous to the printf procedure.
You can assign the output of sprintf to a variable, an expression, or an argument:
sequence s = sprintf( "%s is great!", {"Euphoria"} ) ? s --> {69,117,112,104,111,114,105,97,32,105,115,32,103,114,101,97,116} puts(1, s ) --> Euphoria is great!
When you use sprintf to output a number you effectively convert the number into a string. Here %g is the general number format specifier:
sequence n = sprint( "%g", 3.14 ) pretty:pretty_print(1, n ) --> {51'3',46'.',49'1',52'4'} puts(1, n ) --> 3.14
sprint
The std/text.e library file contains the sprint function which is analogous to the print procedure.
The print procedure outputs a Euphoria object to a device or file; the output shows the encoded values and has braces and commas to show the object structure.
print(1, "mongoose") --> {109,111,110,103,111,111,115,101}
The text:sprint function captures the output of the analogous print procedure as a string. Each digit, each brace, and each comma is encoded as a character.
object z = text:sprint( "mongoose" ) pretty:pretty_print(1, z ) --> -- {123'{',49'1',48'0',57'9',44',',49'1',49'1',49'1',44',',49'1',49'1',48'0', -- 44',',49'1',48'0',51'3',44',',49'1',49'1',49'1',44',',49'1',49'1',49'1',44',', -- 49'1',49'1',53'5',44',',49'1',48'0',49'1',125'}'} puts(1, z ) --> {109,111,110,103,111,111,115,101}
Not Categorized, Please Help
|