String
Strings are defined as an array of characters:
type str = char[]
Of course, this is all internal, and they will be represented to you as "string". However, this will allow you to access the length of the string with <string>.len.
str my_string = "cookie 🍪"
for i in my_string {
print("{my_string[i]}")
}
// c
// o
// o
// k
// i
// e
//
// 🍪
Format strings
You can insert values into format strings using {<value>}:
print("This is a string with a number inside: {5 + 2}")
// This is a string with a number inside: 7
If you'd like to escape the {} characters, you can simply add a backslash \{}:
print("This is an escaped string with an expression inside: \{5 + 2}")
// This is an escaped string with an expression inside: {5 + 2}
All values are converted to strings using the str() function when passed into a format string. If you'd like to use your own format, you can convert your value to a string yourself before passing it into the format string. For example:
fn int_to_float_str(int a) -> str {
return str(float(a))
}
print("My number is {int_to_float_str(10)}")
// "My number is 10.0
Multiline strings
You can declare a multiline string using """. This will also dedent the string to the position of the closing """, and also escape any " inside. For example:
str my_string = """
Hello World
Indented line
Unindented line
"""
print(my_string)
will output
Hello World
Indented line
Unindented line
You can also use values inside multiline strings using the same syntax as described in Format strings. For example:
print("""
3 + 5 = {3 + 5}
8 + 10 = {8 + 10}
""")
will output:
3 + 5 = 8
8 + 10 = 18