> 

 > 

5. 以積木式編程製作Raspberry Pi智能裝置
文本
文本

該文件涵蓋了各種文本處理技術,包括將文本轉換為數字、連接、長度檢查、設置和分解文本、字符搜索、子字符串顯示以及字符代碼的使用。

將文字轉換為數位

解析為數位

{"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>"}

此塊用於將文字轉換為數位。

參數: String (字串)

輸出:

範例代碼:

{"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>"}

輸出:

暫停到數位 - 輸出

連接文字

加入

{"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>"}

此塊用於通過將另一個文本連接在一起來設置一段文本。

參數: String (字串)

輸出: 字串

範例代碼:

{"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>"}

輸出:

連線 - 輸出

檢查文本的長度

長度

{"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>"}

此塊用於顯示文字中的字母數。

參數: String (字串)

輸出:

範例代碼:

{"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>"}

輸出:

長度 - 輸出

設置文字

字串

{"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>"}

此塊用於在塊內設置字母或文本。

參數: String (字串)

輸出: 字串

範例代碼:

{"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>"}

輸出:

String - 輸出

使用分隔符分隔文字

拆分為

{"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>"}

此塊用於根據分隔符分隔文本。

參數: 屏幕專案/字串

輸出: 字串陣列

範例代碼:

{"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>"}

輸出:

在 - 輸出處分割

檢查文字是否包含字元

包括 …

{"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>"}

此塊用於檢查文本是否包含文字的字元。

參數: 屏幕專案/字串

輸出:布林值 (True/False)

範例代碼:

{"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>"}

輸出:

包括 - 輸出

查找字元的位置

尋找索引 …

{"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>"}

此塊用於查找值在文本中首次出現的位置。

參數: 屏幕專案/字串

輸出:

範例代碼:

{"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>"}

輸出:

尋找索引 - 輸出

檢查文本是否為空

是空的

{"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>"}

此塊用於檢查文本是否為空。

參數: 屏幕專案/字串

輸出: 布林值 (True/False)

範例代碼:

{"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>"}

輸出:

為空 - 輸出

以子字串格式顯示文字

從…到長度…的子字串

{"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>"}

此塊用於顯示所選取文字中的子字串。

參數: 屏幕專案/字串

條件: 數字(預設值:0 和 10)

輸出: 字串

範例代碼:

{"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>"}

輸出:

子字串的輸出

顯示兩個文字中的字元順序

將此與…進行比較

{"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>"}

此塊用於顯示兩個文字中字元的順序不同。

參數: 屏幕專案/字串

輸出:

範例代碼:

{"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>"}

輸出:

比較... - 輸出

在特定位置顯示字元

來自…在…

{"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>"}

這個塊用於在指定位置顯示字元。

參數: 屏幕專案/字串

輸出:

範例代碼:

{"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>"}

輸出:

Char from - 輸出

使用字元代碼顯示文字

來自字元代碼的文字

{"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>"}

這個塊用於使用 ASCII 代碼顯示字元。

參數:

輸出: 字串

範例代碼:

{"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>"}

輸出:

來自 Char Code 的文字 - 輸出

目録