The Type string
The type string represents sequences of characters.
A value of type string can be given by a string literal.
A string literal is a text enclosed inside single quotes (') symbols or double quotes (") symbols.
Operations on strings are not the same as operations on numbers.
The operator + joins two or more strings.
Strings have a getter .n which returns the length of the string.
Getters can be applied directly on variables, but not on most other expressions.
To apply a getter on an expression, include it inside parentheses.
Strings can be compared for equality.
The expresson "HA" = "ha" evaluates to false because the comparison is case-sensitive.
Type string has a type alias str. The type alias can be used as an alternative name for the type.
Question: why the string "Learn Programming" doesn't get printed out?
A println statement is required to print out values. To print out the string s2, append the statement:
The getter .str (member of num) converts a number into a string.
Some other types also have a getter .str, for example the type int.
The expression -78 is of type int. The type int has a member .str. The expression (-78).str evaluates to value "-78" of type string. Then, a getter .n can be applied to the string "-78", resulting in the value 3 because the string "-78" has three characters. Therefore, the result of the expression (-78).str.n is the value 3.
While a string can be appended to a string by the operator '+', a number or integer cannot be appended to a string.
To append a value of type num or int to a string, the value must first be converted to type string.
Uncomment the erroneous lines to see the error description.
* * *
-- Converting to string--
There are many ways to convert a value to type string. Most commonly, the getter .str performs the desired conversion, but there are many other possibilities.
When using the println statement, it is usually not necessary to join strings by the operator '+', because arguments can simply be separated by a comma.
When the default output format of println statement is not the desired one, it becomes necessary to perform a custom conversion to type string. Making the printout aligned, which is the topic of the next chapter, is one common reason to perform the custom conversion.
Getter .roman from type int returns a value of type string.
Getter function .pad from type int also returns a value of type string.
Function lastKeyName() also returns a value of type string.
Special sequences are used to put some characters inside string literals.
A special sequence starts with the backslash (\) symbol.
Special sequences are written with two characters, but their result is a single character.
A newline character advances to the next line. It is written with a special sequence \n.
The getter function .cut from type string accepts two arguments which specify the beginning and the end of the resulting string.
The getter functions .cuta, .after, .before, .cutb return the corresponding part of the string.
A print statement can use a different font.
If a font is set, the print statement will start printing at the top left corner of the screen.
Therefore, when a font is set, a new position should also be set.
A font can be set by named argument font.
This program slowly outputs the text of the song.
The break statement ends the current loop.
In this program, the break statement is used to end the while loop when the entire song has been printed out.
The getter .floor from type num was explained in the previous chapter. It converts a number to type int.