© Pradeeka Seneviratne  2019
Pradeeka SeneviratneBBC micro:bit Recipeshttps://doi.org/10.1007/978-1-4842-4913-0_4

4. Working with Text

Pradeeka Seneviratne1 
(1)
Udumulla, Mulleriyawa, Sri Lanka
 

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 ).
    ../images/474604_1_En_4_Chapter/474604_1_En_4_Fig1_HTML.jpg
    Figure 4-1

    Placing the length of block

  • Once finished, your code should look something like this (Figure 4-2 ).
    ../images/474604_1_En_4_Chapter/474604_1_En_4_Fig2_HTML.jpg
    Figure 4-2

    Full code listing

  • The following will be the result.

5

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

awesome
  • 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 ).
    ../images/474604_1_En_4_Chapter/474604_1_En_4_Fig3_HTML.jpg
    Figure 4-3

    Placing the join block

  • 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 ).
    ../images/474604_1_En_4_Chapter/474604_1_En_4_Fig4_HTML.jpg
    Figure 4-4

    Full code listing

  • The following will be the result.

You are awesome

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.

The join block comes with two default strings (Hello World). You can add or remove a text box in the join block by clicking on the Add button (plus icon) or Remove button (minus icon), respectively (Figure 4-5 ).
../images/474604_1_En_4_Chapter/474604_1_En_4_Fig5_HTML.jpg
Figure 4-5

The join block

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

Pear
  • 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 ).
    ../images/474604_1_En_4_Chapter/474604_1_En_4_Fig6_HTML.jpg
    Figure 4-6

    Placing the compare block

  • 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 ).
    ../images/474604_1_En_4_Chapter/474604_1_En_4_Fig7_HTML.jpg
    Figure 4-7

    Full code listing

  • The following will be the result.

-1

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.

Here are some examples that will help you to understand the comparison.
  • 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’.

The compare block has two text boxes to type string1 and string2 (Figure 4-8 ).
../images/474604_1_En_4_Chapter/474604_1_En_4_Fig8_HTML.jpg
Figure 4-8

The compare block

The output is based on the following conditions.
  • 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

As an example, you will take the substring el from the string Hello.
  • 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 ).
    ../images/474604_1_En_4_Chapter/474604_1_En_4_Fig9_HTML.jpg
    Figure 4-9

    The substring of block

  • 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 ).
    ../images/474604_1_En_4_Chapter/474604_1_En_4_Fig10_HTML.jpg
    Figure 4-10

    Full code listing

  • The following will be the result.

el

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.

For example, imagine that you want to get the substring, ‘bees’ from the string ‘Now I see bees I won’.
  • First, give index for characters in the string (Figure 4-11 ).
    ../images/474604_1_En_4_Chapter/474604_1_En_4_Fig11_HTML.jpg
    Figure 4-11

    Indexing a string

  • 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.

The graphical representation of the substring operation can be illustrated as shown in Figure 4-12 .
../images/474604_1_En_4_Chapter/474604_1_En_4_Fig12_HTML.jpg
Figure 4-12

Extracting a part from a string

Figure 4-13 shows the code for the Figure 4-12 built with MakeCode.
../images/474604_1_En_4_Chapter/474604_1_En_4_Fig13_HTML.jpg
Figure 4-13

Substring a string

Here is the list of parameters used for substring of block in the Figure 4-13 .
  • 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.

The following will be the result.
bees

4-5. Getting a Character at a Position

Problem

You want to get a character from a position in the string.

Solution

As an example, you will get the character at the index 1 from the string Hello.
  • 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 ).
    ../images/474604_1_En_4_Chapter/474604_1_En_4_Fig14_HTML.jpg
    Figure 4-14

    The char from block

  • 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 ).
    ../images/474604_1_En_4_Chapter/474604_1_En_4_Fig15_HTML.jpg
    Figure 4-15

    Full code listing

  • The following will be the result.

e

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

As an example, you will convert the string -12.5 to the number value, -12.5.
  • 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 ).
    ../images/474604_1_En_4_Chapter/474604_1_En_4_Fig16_HTML.jpg
    Figure 4-16

    The parse to number block

  • 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 ).
    ../images/474604_1_En_4_Chapter/474604_1_En_4_Fig17_HTML.jpg
    Figure 4-17

    Full code listing

  • The following will be the result.

-12.5

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.

Table 4-1 shows the output for strings with different type of character combinations.
Table 4-1

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

Special Case

If your input string is something like 4e2, the number characters after the e becomes an exponent of 10. The 2 after the e will calculate as 2 powers of ten, which is 10 ∗ 10 or 100. The resulting value then is 4 ∗ 100, which equals 400. Figure 4-18 shows the code for calculating the result for 4e2.
../images/474604_1_En_4_Chapter/474604_1_En_4_Fig18_HTML.jpg
Figure 4-18

Calculating the result for 4e2

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.253.31