> 
 > 
5. Blocks Coding with Raspberry Pi
Text
Text

This documentation covers various text manipulation techniques, including transforming text to numbers, concatenation, length checking, setting and breaking text, character searches, substring displays, and character code usage.

Transforming Text to Number

Parse to number

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><block type=\"string_parsefloat\" disabled=\"true\" x=\"-22\" y=\"247\"><value name=\"text\"><shadow type=\"text\" disabled=\"true\"><field name=\"TEXT\">123</field></shadow></value></block></xml>"}

This block is used to convert a text to a number.

Parameters: String

Output: Number

Sample code:

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><block type=\"pxt-on-start\" x=\"0\" y=\"0\"><statement name=\"HANDLER\"><block type=\"basic_consoleLogNum\"><value name=\"num\"><block type=\"string_parsefloat\"><value name=\"text\"><shadow type=\"text\"><field name=\"TEXT\">123</field></shadow></value></block></value></block></statement></block></xml>"}

The output:

Pause to Number - Output

Concatenating the Text

Join

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><block type=\"text_join\" disabled=\"true\" x=\"-22\" y=\"202\"><mutation items=\"2\"/><value name=\"ADD0\"><shadow type=\"text\" disabled=\"true\"><field name=\"TEXT\">Hello</field></shadow></value><value name=\"ADD1\"><shadow type=\"text\" disabled=\"true\"><field name=\"TEXT\">World</field></shadow></value></block></xml>"}

This block is used to set a piece of text by joining another text together.

Parameters: String

Output: String

Sample code:

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><block type=\"pxt-on-start\" x=\"0\" y=\"0\"><statement name=\"HANDLER\"><block type=\"basic_consoleLogText\"><value name=\"text\"><block type=\"text_join\"><mutation items=\"2\"/><value name=\"ADD0\"><shadow type=\"text\"><field name=\"TEXT\">Hello</field></shadow></value><value name=\"ADD1\"><shadow type=\"text\"><field name=\"TEXT\">World</field></shadow></value></block></value></block></statement></block></xml>"}

The output:

Join - Output

Checking the Length of Text

Length of

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><block type=\"text_length\" disabled=\"true\" x=\"-23\" y=\"112\"><value name=\"VALUE\"><shadow type=\"text\" disabled=\"true\"><field name=\"TEXT\">Hello</field></shadow></value></block></xml>"}

This block is used to show the number of letters in the text.

Parameters: String

Output: Number

Sample code:

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><block type=\"pxt-on-start\" x=\"0\" y=\"0\"><statement name=\"HANDLER\"><block type=\"basic_consoleLogNum\"><value name=\"num\"><block type=\"text_length\"><value name=\"VALUE\"><shadow type=\"text\"><field name=\"TEXT\">Hello</field></shadow></value></block></value></block></statement></block></xml>"}

The output:

Length of - Output

Setting Text

String

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>list</variable><variable>index</variable></variables><block type=\"text\" disabled=\"true\" x=\"-22\" y=\"67\"><field name=\"TEXT\"/></block></xml>"}

This block is used to set a letter or text inside the block.

Parameters: String

Output: String

Sample code:

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><block type=\"pxt-on-start\" x=\"0\" y=\"0\"><statement name=\"HANDLER\"><block type=\"basic_consoleLogText\"><value name=\"text\"><shadow type=\"text\"><field name=\"TEXT\">Hello</field></shadow></value></block></statement></block></xml>"}

The output:

String - Output

Breaking Text with Separators

Split at

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><block type=\"string_split\" disabled=\"true\" x=\"-22\" y=\"337\"><value name=\"this\"><shadow type=\"text\" disabled=\"true\"><field name=\"TEXT\">this</field></shadow></value><value name=\"separator\"><shadow type=\"text\" disabled=\"true\"><field name=\"TEXT\"/></shadow></value></block></xml>"}

This block is used to break the text according to the separators.

Parameters: Screen Item/String

Output: String Array

Sample code:

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>find</variable><variable>list</variable><variable>index</variable></variables><block type=\"pxt-on-start\" x=\"67\" y=\"203\"><statement name=\"HANDLER\"><block type=\"variables_set\"><field name=\"VAR\">find</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"string_split\"><value name=\"this\"><shadow type=\"text\"><field name=\"TEXT\"/></shadow><block type=\"variables_get\"><field name=\"VAR\">list</field></block></value><value name=\"separator\"><block type=\"text\"><field name=\"TEXT\"/></block></value></block></value><next><block type=\"pxt_controls_for\"><value name=\"VAR\"><shadow type=\"variables_get_reporter\"><field name=\"VAR\">index</field></shadow></value><value name=\"TO\"><shadow type=\"math_whole_number\"><field name=\"NUM\">0</field></shadow><block type=\"math_arithmetic\"><field name=\"OP\">MINUS</field><value name=\"A\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"lists_length\"><value name=\"VALUE\"><block type=\"variables_get\"><field name=\"VAR\">find</field></block></value></block></value><value name=\"B\"><shadow type=\"math_number\"><field name=\"NUM\">1</field></shadow></value></block></value><statement name=\"DO\"><block type=\"basic_consoleLogText\"><value name=\"text\"><block type=\"lists_index_get\"><value name=\"LIST\"><block type=\"variables_get\"><field name=\"VAR\">find</field></block></value><value name=\"INDEX\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"variables_get\"><field name=\"VAR\">index</field></block></value></block></value></block></statement></block></next></block></statement></block></xml>"}

The output:

Split at - Output

Checking Whether the Text Contains Characters

Includes …

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>find</variable><variable>list</variable><variable>index</variable></variables><block type=\"string_includes\" disabled=\"true\" x=\"-22\" y=\"382\"><value name=\"this\"><shadow type=\"text\" disabled=\"true\"><field name=\"TEXT\">this</field></shadow></value><value name=\"searchValue\"><shadow type=\"text\" disabled=\"true\"><field name=\"TEXT\"/></shadow></value></block></xml>"}

This block is used to check whether the text contains the characters of the text.

Parameters: Screen Item/String

Output:Boolean (True/False)

Sample code:

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>text</variable></variables><block type=\"pxt-on-start\" x=\"0\" y=\"0\"><statement name=\"HANDLER\"><block type=\"variables_set\"><field name=\"VAR\">text</field><value name=\"VALUE\"><block type=\"text\"><field name=\"TEXT\">Hello World</field></block></value><next><block type=\"controls_if\"><mutation else=\"1\"/><value name=\"IF0\"><shadow type=\"logic_boolean\"><field name=\"BOOL\">TRUE</field></shadow><block type=\"string_includes\"><value name=\"this\"><shadow type=\"text\"><field name=\"TEXT\"/></shadow><block type=\"variables_get\"><field name=\"VAR\">text</field></block></value><value name=\"searchValue\"><shadow type=\"text\"><field name=\"TEXT\">World</field></shadow></value></block></value><statement name=\"DO0\"><block type=\"basic_consoleLogText\"><value name=\"text\"><block type=\"typescript_expression\"><field name=\"EXPRESSION\">\"World is inside the text.\"</field></block></value></block></statement><statement name=\"ELSE\"><block type=\"basic_consoleLogText\"><value name=\"text\"><block type=\"typescript_expression\"><field name=\"EXPRESSION\">\"World is not inside the text.\"</field></block></value></block></statement></block></next></block></statement></block></xml>"}

The output:

Includes - Output

Finding the Position of the Character

Find index of …

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>text</variable></variables><block type=\"string_indexof\" disabled=\"true\" x=\"-22\" y=\"427\"><value name=\"this\"><shadow type=\"text\" disabled=\"true\"><field name=\"TEXT\">this</field></shadow></value><value name=\"searchValue\"><shadow type=\"text\" disabled=\"true\"><field name=\"TEXT\"/></shadow></value></block></xml>"}

This block is used to find the position of the first occurrence of the value in the text.

Parameters: Screen Item/String

Output: Number

Sample code:

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><block type=\"pxt-on-start\" x=\"0\" y=\"0\"><statement name=\"HANDLER\"><block type=\"basic_consoleLogNum\"><value name=\"num\"><block type=\"string_indexof\"><value name=\"this\"><shadow type=\"text\"><field name=\"TEXT\">this</field></shadow></value><value name=\"searchValue\"><shadow type=\"text\"><field name=\"TEXT\">s</field></shadow></value></block></value></block></statement></block></xml>"}

The output:

Find Index of - Output

Checking Text is Empty or Not

Is empty

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><block type=\"string_isempty\" disabled=\"true\" x=\"-22\" y=\"517\"><value name=\"this\"><shadow type=\"text\" disabled=\"true\"><field name=\"TEXT\">this</field></shadow></value></block></xml>"}

This block is used to check whether the text is empty.

Parameters: Screen Item/String

Output: Boolean (True/False)

Sample code:

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><block type=\"pxt-on-start\" x=\"0\" y=\"0\"><statement name=\"HANDLER\"><block type=\"controls_if\"><mutation else=\"1\"/><value name=\"IF0\"><shadow type=\"logic_boolean\"><field name=\"BOOL\">TRUE</field></shadow><block type=\"string_isempty\"><value name=\"this\"><shadow type=\"text\"><field name=\"TEXT\"/></shadow></value></block></value><statement name=\"DO0\"><block type=\"basic_consoleLogText\"><value name=\"text\"><block type=\"typescript_expression\"><field name=\"EXPRESSION\">\"There is nothing inside.\"</field></block></value></block></statement><statement name=\"ELSE\"><block type=\"basic_consoleLogText\"><value name=\"text\"><block type=\"typescript_expression\"><field name=\"EXPRESSION\">\"There is something inside.\"</field></block></value></block></statement></block></statement></block></xml>"}

The output:

Is Empty - Output

Showing Text in Substring Format

Substring of … from … to length …

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><block type=\"string_substr\" disabled=\"true\" x=\"-22\" y=\"517\"><value name=\"this\"><shadow type=\"text\" disabled=\"true\"><field name=\"TEXT\">this</field></shadow></value><value name=\"start\"><shadow type=\"math_number\" disabled=\"true\"><field name=\"NUM\">0</field></shadow></value><value name=\"length\"><shadow type=\"math_number\" disabled=\"true\"><field name=\"NUM\">10</field></shadow></value></block></xml>"}

This block is used to show the substring from the selected text.

Parameters: Screen Item/String

Condition: Number (Default: 0 and 10)

Output: String

Sample code:

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><block type=\"pxt-on-start\" x=\"0\" y=\"0\"><statement name=\"HANDLER\"><block type=\"basic_consoleLogText\"><value name=\"text\"><block type=\"string_substr\"><value name=\"this\"><shadow type=\"text\"><field name=\"TEXT\"/></shadow><block type=\"typescript_expression\"><field name=\"EXPRESSION\">\"Hello World\"</field></block></value><value name=\"start\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow></value><value name=\"length\"><shadow type=\"math_number\"><field name=\"NUM\">10</field></shadow></value></block></value></block></statement></block></xml>"}

The output:

Substring of - Output

Showing the Order of Characters in Two Texts

Compare this to …

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><block type=\"string_compare\" disabled=\"true\" x=\"-22\" y=\"562\"><value name=\"this\"><shadow type=\"text\" disabled=\"true\"><field name=\"TEXT\">this</field></shadow></value><value name=\"that\"><shadow type=\"text\" disabled=\"true\"><field name=\"TEXT\"/></shadow></value></block></xml>"}

This block is used to show the order of characters in two texts is different.

Parameters: Screen Item/String

Output: Number

Sample code:

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><block type=\"pxt-on-start\" x=\"0\" y=\"0\"><statement name=\"HANDLER\"><block type=\"basic_consoleLogNum\"><value name=\"num\"><block type=\"string_compare\"><value name=\"this\"><shadow type=\"text\"><field name=\"TEXT\"/></shadow><block type=\"typescript_expression\"><field name=\"EXPRESSION\">\"Hello World\"</field></block></value><value name=\"that\"><shadow type=\"text\"><field name=\"TEXT\">Hello</field></shadow></value></block></value></block></statement></block></xml>"}

The output:

Compare... - Output

Displaying Character at the Certain Position

Char from … at …

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><block type=\"string_get\" disabled=\"true\" x=\"-22\" y=\"652\"><value name=\"this\"><shadow type=\"text\" disabled=\"true\"><field name=\"TEXT\">this</field></shadow></value><value name=\"pos\"><shadow type=\"math_number\" disabled=\"true\"><field name=\"NUM\">0</field></shadow></value></block></xml>"}

This block is used to show the character at the specified position.

Parameters: Screen Item/String

Output: Number

Sample code:

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><block type=\"pxt-on-start\" x=\"0\" y=\"0\"><statement name=\"HANDLER\"><block type=\"basic_consoleLogText\"><value name=\"text\"><block type=\"string_get\"><value name=\"this\"><shadow type=\"text\"><field name=\"TEXT\">this</field></shadow></value><value name=\"pos\"><shadow type=\"math_number\"><field name=\"NUM\">3</field></shadow></value></block></value></block></statement></block></xml>"}

The output:

Char from - Output

Using Character Code to Show Text

Text from char code

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><block type=\"stringFromCharCode\" disabled=\"true\" x=\"-22\" y=\"697\"><value name=\"code\"><shadow type=\"math_number\" disabled=\"true\"><field name=\"NUM\">0</field></shadow></value></block></xml>"}

This block is used to show the character using the ASCII code.

Parameters: Number

Output: String

Sample code:

{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><block type=\"pxt-on-start\" x=\"0\" y=\"0\"><statement name=\"HANDLER\"><block type=\"basic_consoleLogText\"><value name=\"text\"><block type=\"stringFromCharCode\"><value name=\"code\"><shadow type=\"math_number\"><field name=\"NUM\">101</field></shadow></value></block></value></block></statement></block></xml>"}

The output:

Text from Char Code - Output

Table of Content