The Type string [1/14]
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?
Answer (click):
A println statement is required to print out values. To print out the string s2, append the statement:
println s2;
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 a println statement, it is usually not necessary to join strings by an 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.
This program prints out the name of the last key pressed whenever a different key is pressed. It uses the function lastKeyName(). The variable prevKeyName stores the name of the previously pressed key.
Does anything get printed out when the same key is pressed multiple times in a row?
After running and analyzing this program, change it so that it outputs the name of the key even if the same key is pressed multiple times in a row. You need to use the function isPressed(), which returns a value of type bool, to detect when a key is pressed or released. Therefore, even if a user is holding a key down for long time, the program should print out the key name only once. When the same key is released and then pressed down again, the key name should be printed out again.
Solution, part 1:
At the beginning, add: #bool wasPressed=false;
Also, delete the entire if statement together with the println sub-statement.
Solution, part 2:
In the loop, add: #bool isPressedNow = isPressed(keyName);
Solution, part 3:
In the loop, add: if isPressedNow and not wasPressed :println keyName;
Solution, final part:
In the loop, add at end: wasPressed = isPressedNow;
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.
After running and analyzing the given program, add one more println statement that prints out the text 'Up above the world so high' and 'Like a diamond in the sky', where those two lines of the song should be displayed as separate lines. Note: use only one println statement.
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.
After running and analyzing the given program, add one more println statement that prints out the text 67890 by applying the getter function .cut on variable i.
After that, add one more println statement that prints out the text 23456789012 by applying the getter function .cutb on variable i.
After that, add one more println statement that prints out the text 23456789012 by applying the getter function .after on variable i.
After that, add one more println statement that prints out the small letter 'h' by applying the getter function .cut on variable s.
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.
After running and analyzing this program, try modifying the literals 25 and 18 to find out how they affect the output of the program.
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.
After running and analyzing this program, try modifying the literal 20 in the expression that sets the initial value of variable n. Find out what property of the program is affected by this literal.
After that, use a println statement at the end of the loop to print out the value of variable n.
After that, use a named argument ln to set the particular printout line, for example line 17.
After that, use a println statement to print out the value of variable lineA before the loop. Don't forget to use a sleepMs function afterwards, otherwise this printout will get immediately deleted. After that , use a println statement to print out the value of variable lineB before the loop.
<< F2:Prev - - F4:Next >>