Text
text is the type for string of characters in Scriptol.
This is an object with methods.
A literal text is a string of characters enclosed between simple
or double quotes.
There is a difference between the two notations, this will be explained
further.
Syntax:
text s s = "str" s = s[i] s[i] = s2 s = s[i..j] s[i..j] = s s[i..j] = "" |
creates a text. initializes. gets a char. replaces a char, s2 should be a one-char text. gets a sub-string, from i until j included. replaces a sub string. removes a sub string. |
Concatening
The + sign may be used also on texts, and it performs concatening two texts
into a single one.
Examples: | text
t = "prefix" print t + "suffix" |
Displays: | > prefixsuffix |
Multi-lines
If a text if too long, and can't fit in the screen, the + symbol allows
to split it:
Example:
text t = "this is a text rather " +
"long"
In fact it is possible to write a literal text in several lines and to assign
it or use it inside an expression:
text t = "this is a text rather "
"long"
Quotes inside text
Supposing you want assign the a variable a with exactly this text:
my "pretty" car.
You can't put it inside quotes:
a = "my "pretty" car"
The compilater is not able to process that! A special code is used to insert
quotes inside a text, not as delimiter, this is "\"
a = "my \"pretty\" car"
You can also alternate the type of quotes:
a = "my 'pretty' car"
or
a = 'my "pretty" car'
But this works when target is PHP, not in C++.
Escaping code
To insert characters that can't be inserted directly into a text, the antislash
is used: \
\n this is an end of line.
\t this is a tab.
To insert a simple quote, write:
"abc\'def"
\" | insert a double quote. |
\' | insert a single quote. |
\n | end of line code. |
\t | tab code. |
\\ | insert the antislash. |
The $ sign has a special function, inside double quotes only. It allows to insert the
content of a variable when the target is Php.
Example of variable in text: | text
t = "demo" a = "some $t" print a |
Displays: | > some demo |
To display the $ sign, when the target language is Php (this works in C++
also), use the simple quotes instead:
print '$demo'
displays: $demo
That is the same with the { } signs.
Textual expressions
The text operators are:
=, <, >, <=, >=
+ [] in |
comparing two texts. returns 0 if identical. -1 if the first text is alphabetically before. 1 if the first text is alphabetically after. concatening two texts. indexing, slicing ou splicing tests if a un text is included into another one. |
Text methods
The methods apply to a variable or a literal with the form:
"string".method()
Return | Method | Function |
text | capitalize() | converts the first char to uppercase. |
int | compare(text) | compares lexicographically two texts (ignore case). return -1, 0, 1. |
text | dup(int) | returns a text duplicated n times. Ex: "*".dup(10). |
void | fill(text, int) | fills s with text n times. |
int | find(text s2) | returns the position of text s2 inside the text.
returns "nil" if no found. |
int | identical(text) | compares, doesn't ignore case. Return -1, 0, 1 |
void | insert(int, text) | inserts a text at the given position. |
boolean | isNumber() | returns true if the text is a numeric string. |
int | len length() |
returns the length. |
text | lower() | converts to lowercase. |
text | lTrim() | removes heading controls/blanks. |
text | replace(ft, rt) | replaces each occurence of ft by rt. |
void | reserve(int) | allocates the given size to make a buffer. |
text | rTrim() | removes trailing controls/blanks. |
array | split(sep) | splits a text into items separated by sep. |
int | toInt() | casts to function. |
natural | toNatural() | converts to integer. |
real | toReal() | converts to real. |
text | toText() | converts a literal string to text (for C++). |
text | trim() | removes heading and trailing control codes/blanks. |
text | upper() | converts to uppercase. |
text | wrap(int size) | wordwraps the text. |
Exercises |
1) Capitalize the first letter for each word in the following text: |