Strings
Tutorial about how to use strings in MIFL.
Strings
Strings and lists are similar to ActiveScript's strings and arrays.
They are very different from strings in C and C++; C/C++ don't
even contain lists.
To create a string in code, just use quotes around some text,
such as "Hello world!".
String operators
Many of the operators work with strings:
-
x = "Hello" + " there!" - This fills x
with "Hello there!", concatenating two or
more strings together.
-
x += "Hello" - This is interpreted as "x = x + "Hello"".
-
"abacus" < "zebra" - The <, <=,
> >=, ==, and === operators do a case sensative comparison
of the two strings. You need to use StringCompare() to do a case
insensative compare.
-
x = "Hello"[2] - Fills x in with the 3rd character (0 being the
first character). x = 'l'.
-
x = "Hello"; x[2] = 'a'; - This fills x with "Hello",
but then changes the 3rd character to 'a', resulting
in "Healo". If the index is beyond the end of the string, the
string will be extended with spaces.
Depending upon the operator and the location of the string
(whether it's to the left or right of the operator), strings will
be converted to other data types (such as numbers) or other
data types will be converted to strings.
Some examples:
For more details on type conversions, see the reference section.
String "methods"
Strings support "methods", which are functions that automatically
modify the string that they appear after. Example: To use the
method "StringAppend" with the string in variable x,
just use "x.StringAppend()".
Below is a partial list of methods supported by strings.
A complete list can be found under the "Reference" section of
help, in "Data types", "Strings".
-
StringAppend - Append one string onto the end of another.
-
StringCompare - Case insensative comparison between strings.
-
StringInsert - Insert one string into another.
-
StringLength - Returns the number of characters in a string.
-
StringSearch - Search through the string for a matching string.
-
StringSlice - Cut a portion of the string out and into a new string.
String escape sequences
If you wish to include quotes within a string, you'll need to
use some "escape sequences":
\" |
Places a quote in the string.
Example: "This is a \"quote\"." will be displayed as This is a "quote".
|
\' |
Single quote.
|
\n |
Newline. Places a line-break between characters.
|
\r\n |
Alternate way of writing a newline.
|
\t |
Tab separator.
|
\\ |
Backslash
|
\ddd |
Octal character, where d is a digit from 0..7.
|
\xddd |
Hex character, where d is a digit from 0..f.
|
String reference counting
When you create a string, in the background MIFL allocates some
memory for the string. Associated with the memory is a "reference count"
that lets MIFL know if the string is still remembered by
any variables, parameters, properties, etc. If the MIFL
script "forgets" about the string then the memory is automatically
deleted. This ensures that you won't get memory leaks from stings
that are no longer in use.
It also causes some noteworthy behavior. Look at the following
code:
var x = "Hello world!";
var y = x;
x.StringAppend ("more text");
return y;
|
This function will return a string, "Hello world!more text".
Here's why:
-
"var x = "Hello world!"" creates a string and then stores
a reference to it in x.
-
"var y = x" does not copy the string, but copies
the reference to the string. Therefore, x and y refer
to the same string.
-
"x.StringAppend("more text")" appends "more text" onto the
string that both x and y are referencing.
-
"return y" just returns another reference.
This can get you if you're not careful because you may assume
that "y = x" will copy the string, but it only copies the
reference. Then, if you modify x you'll find that y is also
modified, or vice versa.
(Conversely, this can be a very useful feature.)
If you want y to contain a copy of the string (so that modifying
it won't affect x) then use:
This duplicates the string referred to by variable x,
and sets y to the refer to the new string.
Note: Concatenation with the + operator creates a
copy of the strings, which is different behavior than StringAppend(),
which modifies a string in place.
Share with your friends: |