MakeCode has many blocks to offer when it comes to manipulating text strings. In this chapter you will learn how to find the length of a text, joining together any number of pieces of text, comparing two strings, extracting a part from a string, converting a string to a number, and extracting a character from a string at the specified index.
4-1. Finding the Length of a Text
Problem
You want to find the length of a text.
Solution
In the Toolbox, click on the Basic category. Then click and drag the show number block over, and place it inside of the on start block.
In the Toolbox, click on Advanced to expand the category list, and then click on the Text category.
- Click and drag the length of block over and place it inside of the show number block (Figure 4-1 ).
- Once finished, your code should look something like this (Figure 4-2 ).
The following will be the result.
How It Works
The length of block returns the number of letters, including spaces in the provided text as an integer. Therefore, you must use the show number block with the length of block to show the output on the micro:bit display.
4-2. Joining Strings
Problem
You want to join two or more strings together to create a piece of text.
Solution
As an example, you will join the following piece of strings to create a text.
You
are
In the Toolbox, click on the Basic category. Then click and drag the show string block over, and place it inside of the on start block.
In the Toolbox, click on Advanced to expand the category list, and then click on the Text category.
- Click and drag the join block over and place it inside of the show string block (Figure 4-3 ).
In the join block, click on the first text box and type the string You followed by a space. Then, click on the second text box, and type the string are followed by a space.
Click on the Add button (plus icon) to add a new text box (third text box).
In the third text box, type the string awesome.
- Once finished, your code should look something like this (Figure 4-4 ).
The following will be the result.
How It Works
The join block creates a piece of text by joining together any number of strings. It always returns a string. Therefore, you should place it inside a show string block to direct the output to the micro:bit display.
4-3. Comparing Two Strings
Problem
You want to compare two strings based on which characters are first.
Solution
As an example, you will compare the following two strings.
Apple
In the Toolbox, click on the Basic category. Then click and drag the show number block over, and place it inside of the on start block.
In the Toolbox, click on Advanced to expand the category list, and then click on the Text category.
- Click and drag the compare block over and place it inside of the show number block (Figure 4-6 ).
In the compare block, click on the first text box and type in the string Apple. Then, click on the second text box and type in the string Pear.
- Once finished, your code should look something like this (Figure 4-7 ).
The following will be the result.
How It Works
The two strings are compared based on the order of their characters in ASCII encoding. The complete ASCII encoding table with the English alphabet for micro:bit can be found in Appendix A.
The string ‘A’ is less than ‘B’ because ‘B’ comes after the ‘A’.
The string ‘TIGER’ is greater than ‘LION’ because ‘T’ comes after the ‘L’.
The string ‘Tiger’ is less than ‘tiger’ because ‘t’ comes after ‘T’.
The string ‘100’ is greater than ‘Camel’ because ‘C’ comes after ‘1’.
If string1 is greater than string2, it returns 1.
If both the strings are equal lexicographically, it returns 0.
If string1 is less than string2, it returns -1.
You can use the compare block with show number or show string block to direct the output to the micro:bit display.
4-4. Making Substrings
Problem
You want to take some part from a string to make a smaller string.
Solution
In the Toolbox, click on the Basic category. Then click and drag the show string block over, and place it inside of the on start block.
In the Toolbox, click on Advanced to expand the category list, and then click on the Text category.
- Click and drag the substring of block over, and place it inside of the show string block (Figure 4-9 ).
In the substring of block, click on the first text box and type in the string Hello. Then, click on the second text box and type in the value 1. Finally, click on the third text box and type in the value 2.
- Once finished, your code should look something like this (Figure 4-10 ).
The following will be the result.
How It Works
The substring of block can be used to get part of a string. The length of a string is the number of characters it contains, including spaces, punctuation, and control characters. The index of the first character is 0, the second character is 1, and so on. The index of the last character is (length of string) -1.
The first parameter of the substring of block accepts the string. The second parameter accepts the index of the first character of the substring. The third parameter accepts the number of characters in the substring, including spaces, punctuation, and control characters.
- First, give index for characters in the string (Figure 4-11 ).
Then find the index of the first letter of the substring, which is 10.
Finally, count the number of characters in the substring, which is 4.
First parameter (substring of) - complete string, which is Now I see bees I won.
Second parameter (from) - index of the first character of the substring, which is 10.
Third parameter (of length) - number of characters in the substring, which is 4.
4-5. Getting a Character at a Position
Problem
You want to get a character from a position in the string.
Solution
In the Toolbox, click on the Basic category. Then click and drag the show string block over, and place it inside of the on start block.
In the Toolbox, click on Advanced to expand the category list, and then click on the Text category.
- Click and drag the char from block over, and place it inside of the show string block (Figure 4-14 ).
In the char from block, in the first text box, type in the string Hello. In the second text box, type in the number 1.
- Once finished, your code should look something like this (Figure 4-15 ).
The following will be the result.
How It Works
The char from block returns the character at the specified position of any string. The position is known as the index. The index of the first character of the string is 0, the second character is 1, and so on. The index of the last character is (length of string) -1.
The first parameter of the char from block accepts the input string. The second parameter accepts the index of the character that you want to return. A character could be a space, punctuation, or control character.
If you are provided a number that is out of index or negative, the micro:bit display doesn’t output anything.
4-6. Converting a String to a Number
Problem
You want to convert a string consisting of number characters to a number value.
Solution
In the Toolbox, click on the Basic category. Then click and drag the show number block over, and place it inside of the on start block.
In the Toolbox, click on the Advanced to expand the category list and then click on the Text category.
- Click and drag the parse to number block over and place it inside of the show number block (Figure 4-16 ).
In the parse to number block, click on the text box and type in the string -12.5.
- Once finished, your code should look something like this (Figure 4-17 ).
The following will be the result.
How It Works
The parse to number block allows you to convert a string consisting of number characters into a floating-point number value. The input string can also have a ‘-’ (minus) and ‘.’ (decimal point) symbol. If the first character of the string is the minus symbol, the string will convert into a negative floating-point number value. If your string is something like 123abc, the numeric part will convert to the numeric value, which is 123. If the string is something like abc123, you will get a NaN (Not a Number) error, known as an exception, on the micro:bit display.
Output for strings with different type of character combinations
String | Output |
---|---|
123 | 123 |
abc | NaN |
123abc | 123 |
abc123 | NaN |
a123bc | NaN |
12 3 | 12 |
12-3 | 12 |
1.23 | 1.23 |
12/3 | 12 |