Notice: Stat is currently in private beta. This documentation is incomplete and subject to change.
Stat Docs
Strings in Stat
Strings store text values. A string is a series of 0 or more characters like
"Greetings Earthlings".
Strings are UTF-8 compatible and can store any character including multi-byte characters like pi π or emojis like 🍉. In Stat, the syntax for strings is powerful enough for seasoned programmers yet simple enough for beginning programmers.
You can define a string by enclosing a series of characters in double quotes like so:
"Hello World".
You can also concatenate two strings by using the + operator between the two strings. You can also embed
strings into other strings using a technique commonly referred to as string interpolation. String interpolation allows
you to handle creating complex string values with ease. You can even extract parts of a string by specifying a range
accessor. String Literals
The most basic way to create a string is to hard code a string literal directly in your code. A string literal is a sequence
of 0 or more characters surrounded by double quotation marks
". Here's an example: MAIN // This is how you create a string literal // This statement doesn't do anything // other than create the string literal, but this // is a valid statement, albeit a bit useless "Hello World"
The code above doesn't do anything other than create a string literal, then forgets about it.
You'd be better served by storing that string literal into a variable so that it can be referred to later
or so that you can pass it to a function or a stream. Here's a more useful example:
MAIN let greeting = "Hello World" // This is a zero length string // not to be confused with the empty value let blank = ""
Escape characters
You can embed common special characters into strings by escaping them with a back slash. These are referred to as string
escape characters. Here is a list of all the escape characters you can add to strings:
\0- The null character (Not to be confused with the empty value... that's different)\\- A literal back slash. To put a single back slash into a string, prefix it with another back slash.\t- A tab character.\f- A form feed character. Otherwise known as the new page character.\v- A vertical tab character.\n- A newline character.\r- A carriage return character.\e- An escape character. Same as\x1bin binary\"- A literal double quote character.
Here's an example of embedding some of these escape characters in a string literal
MAIN /* This ends up being: He said: "Hello There" Line 2 */ let stringVal = "He said: \"Hello There\"\nLine 2"
Unicode Character Escapes
In addition to the common escape characters, you can also embed unicode characters in a string by specifying unicode character escapes.
A unicode character is a character that can take up multiple bytes, but is displayed as a single character in a string.
The syntax for unicode character escapes is
\u{n} where n is a 1-6 digit hexadecimal number that specifies
the unicode code point. Valid values range from 0 to 10ffff. For example, to specify the pi π character
use this: \u{3c0}. Here is an example of strings with embedded unicode character escapes: MAIN // These 2 variables store the same string // It's just 2 different ways to write the same thing let stringVal = "Would you like a piece of π?" let duplicate = "Would you like a piece of \u{3c0}?" // Here are some other unicode character examples // Copyright © let copyRight = "\u{a9}" // Water mellon. 🍉 let waterMellon = "\u{1f349}" // Puppy dog. 🐶 let puppyDog = "\u{1f436}"
Multi-line Strings
All strings can be multi-line. There is no special way to code a multi-line string, no "here doc" or triple quotes...
nope, just code it like you'd expect. The only thing to be aware of with multi-line strings is that even they must
follow the correct indentation rules. The indentation rule for multi-line strings is that each subsequent line must
be indented 1 more time from the first line of the string. It's better to just show you some examples:
MAIN let paragraph = "Line 1 Line 2 Line3" // The same thing but with escapes let paragraph2 = "Line1\nLine2\nLine3" if true // Another paragraph but with a trailing new line character let paragraph3 = "Bees play a crucial role in the global ecosystem. " // The same thing but with escapes let paragraph4 = "Bees play a crucial\nrole in the\nGlobal ecosystem.\n" // One more example with indentation let indentationExample = " This is actually the 2nd line This is the 3rd line and it begins with 4 spaces This is the 4th line and does not start with any white-space"
Here's a quick illustration on how multi-line indentation works.

String Interpolation
String interpolation is a technique used to embed string values into string literals. This makes it easier to assemble complex strings
without having to use concatenation. Stat has a very powerful string interpolation interpreter that allows you to not only embed
variables, but complex expressions including function calls and even nested strings with their own string interpolations.
To embed a value inside a string, use the following syntax:
\{value}. Here are some examples: IMPORTS getBalance MAIN let name = "John" let greeting = "Hello \{name}. How are you?" // You can use any value that evaluates to a // string, not just variables. This uses a function call let balance = "$\{getBalance()}.00" // You can even use other string literals // Obviously, you could just use John in the string itself // but the purpose of this example is to show you what's possible greeting = "Hello \{"John"}" // You can even nest interpolations like this greeting = "Hello \{name + ". Your balance is: $\{getBalance()}.00"}"
String Mutability
Everything in Stat is immutable and strings are no exception which means that once they are created, they cannot be changed.
However, just because a string cannot be changed, doesn't mean that you can't assign an entirely different string to the same variable.
We need to make a distinction here because there has been plenty of debate on what mutability vs immutability actually is and how it works in different languages.
In Stat, the underlying value is immutable. But variables can be re-assigned at will. When you re-assign a variable,
the underlying value remains the same and any variable that still references that value doesn't change. Let's explain with some code examples:
MAIN let name = "John" // Now both name and name2 point to "John" let name2 = name // Here, the underlying value "John" doesn't change // What happens is that name simply points to a new, different string // Now, name points to "Jane" and name2 points to "John" name = "Jane" // After this statement, both name2 and name3 point to "John" // It's important to know that there are 3 defined variables // but only 2 underlying strings... "John" and "Jane" // These underlying strings never change even though the // variables that point to these strings may change throughout // the life of the program let name3 = name2
Concatenating Strings
Concatenating strings means to combine them into a new joined string. You can concatenate 2 or more strings with the addition operator
+. MAIN let firstName = "John" let lastName = "Doe" let fullName = firstName + " " + lastName // Now fullName is "John Doe"
You can also append a string to another string by using the augmented assignment addition operator
+=. MAIN let name = "John" name += " Doe" // Now name is "John Doe"
Repeating Strings
Stat provides a very simple way to repeat a string. Simply multiply it by a positive integer.
Here is an example:
MAIN let john = "John" let longJohn = john * 3 // Now, longJohn is "JohnJohnJohn" longJohn *= 2 // Now longJohn is "JohnJohnJohnJohnJohnJohn"
Getting the length of a string
Strings have 2 types of length properties.
charLength and byteLength. In normal ASCII strings that don't contain any
special characters, these 2 properties will be the same, but for strings with multi-byte characters, the 2 properties will be different. MAIN let name = "John" // This will be 4 let strLen = name.charLength // This will also be 4 let byteLen = name.byteLength // Lets define a string with multi-byte characters let question = "Would you like a slice of 🍉?" // This will be 28 strLen = question.charLength // This will be 31 because 🍉 takes up 4 bytes byteLen = question.byteLength
Accessing Substrings
A substring is a shorter part of a string like possibly a word within a sentence. To access a substring simply use an accessor using brackets
[] To get a single character use an integer index like so:
stringVar[3]. This gets the 3rd character. Did you notice something different? In other
languages, an index of 3 usually means the 4th character, but not in Stat. In Stat, indexes are 1 based, which means that they start at 1. To get the first character
in a string, use an index of 1. To get the 5th character, use an index of 5, etc... You can also use negative indexes to go backward from the end of the string.
To get the last character in a string, use an index of -1. To get the 3rd to last character use an index of -3. If you use an index of 0, an empty string will
be returned. Also, indexes respect multi-byte characters so a character that takes up 4 bytes only takes up a single index in the string. MAIN let greeting = "Hello World" // The letter "W" let char = greeting[7] // The letter "d" char = greeting[-1] // If you try to access an index outside // the bounds, you'll get an empty string char = greeting[20] // Now, char is a zero character string char = greeting[-20] // Still a zero character string greeting = "Slice the 🍉 for me" // The letter "r" char = greeting[15] // The water mellon "🍉" char = greeting[11] // The water mellon again char = greeting[-8] // A zero character string // There is no 0th character char = greeting[0]
Something to note
In Stat, there is no such thing as achar or a single character data type. Even a single character is a string. To get a range of characters within a string, use a range accessor like so:
stringVar[3..7]. This gets characters 3 through 6 - a 4 character substring. MAIN let greeting = "Hello World" // "Wor" let sub = greeting[7..10] // Use an inclusive range to include the last // index in the range. This is "Worl" sub = greeting[7...10] // You can use negative ranges too. // This ends up being "llo" sub = greeting[-9..-6] // Use an open range to start at an index, // and get the rest of the string // This will be: "World" sub = greeting[7..] // You can also use the infinity constant sub = greeting[7..infinity] // To get a string all the way until a certain // number of characters before the end, // use a positive index to start, and a // negative index as the end. // This ends up being "Hello Wor" sub = greeting[1..-2]
Comparing Strings
You can compare strings using either the "equality" operator
== or the "is" operator === There is no difference between the 2 operators because Stat treats primitive values as the same if they are equal to each other.
This means that 2 strings are the same if they contain exactly the same characters. MAIN let greeting = "Hello World" let salutations = "Hello World" // Evaluates to true let equal = greeting == salutations // Also evaluates to true equal = greeting === salutations
Previous
Next