String
A String
value represents a sequence of characters.
Creating strings
A string can be created by wrapping the characters in double quotes:
Strings are immutable, this means that whenever you want to modify a string you'll get a new one and the original one will remain unchanged.
String length
You can use the size operator #
to retrive the size of a String
:
Strings concatenation
A String
can be created by concatenating two other String
values by using the concatenation operator ..
:
Accessing individual characters
The indexing operator can be used to read individual characters in a String
:
note
Given that strings are immutable, you can't change a character by using indexed assignment.
Accessing with ranges
You can use slicing to extract a portion of a string,
much as you would with an Array
value:
String manipulation
Most string manipulation functions can be found in the string
core module.
VM Internals
Internalization
Strings are internalized. This means that up to a certain length, if the value is the same, the same exact string in memory will be used for each content. This makes comparing small strings very fast:
In the example above the VM will simply compare the memory location of the two strings, a simple integer comparison, knowing that the string "short"
is internalized. If the strings are the same their location in memory must match.
Wide-string support
Currently wide strings are not supported. This means that while you can output multi-byte encoded character (such as emojis), Melon will see the multi-byte components as distinct characters. While this may not be a big issue in some cases, it's something to be aware of when doing string manipulation as it may break the string (eg: a multi-byte sequence is interrupted).
This is also why if you use the size operator #
on a multi-byte char it will not return 1
.