Arduino - 字串

字串用於儲存文字。它們可用於在 LCD 或 Arduino IDE 序列監視器視窗中顯示文字。字串對於儲存使用者輸入也很有用。例如,使用者在連線到 Arduino 的鍵盤上鍵入的字元。

Arduino 程式設計中有兩種型別的字串 -

  • 字元陣列,與 C 程式設計中使用的字串相同。
  • Arduino String,它允許我們在草圖中使用字串物件。

在本章中,我們將學習字串,物件以及 Arduino 草圖中字串的使用。在本章結束時,你將瞭解在草圖中使用哪種型別的字串。

字串字元陣列

我們將學習的第一種字串是由 char 型別的一系列字元組成的字串。在上一章中,我們瞭解了陣列是什麼,它是儲存在記憶體中的相同型別變數的連續系列。字串是 char 型別的陣列。

字串是一個特殊的陣列,在字串的末尾有一個額外的元素,它總是具有 0(零)值。這稱為空終止字串

字串字元陣列示例

此示例將說明如何建立字串並將其列印到序列監視器視窗。

void setup() {
   char my_str[6]; // an array big enough for a 5 character string
   Serial.begin(9600);
   my_str[0] = 'H'; // the string consists of 5 characters
   my_str[1] = 'e';
   my_str[2] = 'l';
   my_str[3] = 'l';
   my_str[4] = 'o';
   my_str[5] = 0; // 6th array element is a null terminator
   Serial.println(my_str);
}

void loop() { 

}

以下示例顯示了字串的組成部分; 帶有可列印字元的字元陣列,0 表示陣列的最後一個元素,表示這是字串結束的位置。可以使用 Serial.println() 將字串列印到 Arduino IDE Serial Monitor 視窗。

同樣的例子可以用更簡單的方式編寫,如下所示 -

void setup() {
   char my_str[] = "Hello";
   Serial.begin(9600);
   Serial.println(my_str);
}

void loop() {

}

在此草圖中,編譯器計算字串陣列的大小,並且自動用 null 將字串終止。長度為 6 個元素的陣列由 5 個字元後再跟 0 組成。

操作字串陣列

我們可以在草圖中更改字串陣列,如下圖所示。

void setup() {
   char like[] = "I like coffee and cake"; // create a string
   Serial.begin(9600);
   // (1) print the string
   Serial.println(like);
   // (2) delete part of the string
   like[13] = 0;
   Serial.println(like);
   // (3) substitute a word into the string
   like[13] = ' '; // replace the null terminator with a space
   like[18] = 't'; // insert the new word
   like[19] = 'e';
   like[20] = 'a';
   like[21] = 0; // terminate the string
   Serial.println(like);
}

void loop() {

}

結果

I like coffee and cake
I like coffee
I like coffee and tea

草圖按下面的方式工作。

建立和列印字串

在上面給出的草圖中,將建立一個新字串,然後列印該字串以在“Serial Monitor”視窗中顯示。

縮短字串

通過用空終止符(2)來替換字串中的第 14 個字元來縮短字串。它的陣列索引為 13。

列印字串時,所有字元都列印到新的 null 終止符。其他字元不會消失; 它們仍然存在於記憶體中,字串陣列仍然是相同的大小。唯一的區別是任何使用字串的函式只能看到第一個空終止符的字串。

更改字串中的字元

最後,草圖將 cake 改為 tea (3)。它首先必須使用空格替換類似[13]的 null 終止符,以便將字串恢復為最初建立的格式。

新字元用 tea 一詞覆蓋 cake 中的 cak。這是通過覆蓋單個字元來完成的。 cakee 被替換為新的 null 終止字元。結果是字串實際上以兩個空字元結束,原始字串在字串的末尾,而新的 null 字元替換 cake 中的 e。列印新字串時沒有區別,因為列印字串的函式在遇到第一個空終止符時會停止列印字串字元。

操作字串陣列的函式

前面的草圖通過訪問字串中的單個字元以手動方式操作字串。為了更容易操作字串陣列,你可以編寫自己的函式來執行此操作,或者使用 C 語言庫中的某些字串函式。

下面給出了操作字串陣列的函式列表

下一個草圖使用了一些 C 字串函式。

void setup() {
   char str[] = "This is my string"; // create a string
   char out_str[40]; // output from string functions placed here
   int num; // general purpose integer
   Serial.begin(9600);

   // (1) print the string
   Serial.println(str);

   // (2) get the length of the string (excludes null terminator)
   num = strlen(str);
   Serial.print("String length is: ");
   Serial.println(num);

   // (3) get the length of the array (includes null terminator)
   num = sizeof(str); // sizeof() is not a C string function
   Serial.print("Size of the array: ");
   Serial.println(num);

   // (4) copy a string
   strcpy(out_str, str);
   Serial.println(out_str);

   // (5) add a string to the end of a string (append)
   strcat(out_str, " sketch.");
   Serial.println(out_str);
   num = strlen(out_str);
   Serial.print("String length is: ");
   Serial.println(num);
   num = sizeof(out_str);
   Serial.print("Size of the array out_str[]: ");
   Serial.println(num);
}

void loop() {

}

結果

This is my string
String length is: 17
Size of the array: 18
This is my string
This is my string sketch.
String length is: 25
Size of the array out_str[]: 40

草圖以下列方式工作。

列印字串

新建立的字串將列印到“Serial Monitor”視窗,如先前草圖中所做。

獲取字串的長度

strlen() 函式用於獲取字串的長度。字串的長度僅適用於可列印字元,不包括空終止符。

該字串包含 17 個字元,因此我們在"Serial Monitor"視窗中看到 17 個字元。

獲取陣列的長度

運算子 sizeof() 用於獲取包含字串的陣列的長度。長度包括空終止符,因此長度比字串的長度多一個。

sizeof() 看起來像一個函式,但從技術上講是一個操作符。它不是 C 字串庫的一部分,但在草圖中用於顯示陣列大小和字串大小(或字串長度)之間的差異。

複製字串

strcpy() 函式用於將 str []字串複製到 out_num []陣列。 strcpy() 函式將傳遞給它的第二個字串複製到第一個字串中。字串的副本現在存在於 out_num []陣列中,但只佔用陣列的 18 個元素,因此我們在陣列中仍然有 22 個 char 元素。這些元素在記憶體中的字串之後找到。

字串被複制到陣列中,以便我們在陣列中有一些額外的空間用於草圖的下一部分,即在字串的末尾新增一個字串。

將字串附加到字串(連線)

草圖將一個字串連線到另一個字串,這稱為連線。這是使用 strcat() 函式完成的。 strcat() 函式將傳遞給它的第二個字串放到傳遞給它的第一個字串的末尾。

連線後,列印字串的長度以顯示新的字串長度。然後列印陣列的長度,以顯示我們在 40 個元素的長陣列中有一個 25 個字元的長字串。

請記住,25 個字元的長字串實際上佔用了陣列的 26 個字元,因為 null 終止為零。

陣列邊界

使用字串和陣列時,在字串或陣列的範圍內工作非常重要。在示例草圖中,建立了一個長度為 40 個字元的陣列,以便分配可用於操作字串的記憶體。

如果陣列太小並且我們試圖將比該陣列大的字串複製到該陣列,則該字串將被複制到陣列的末尾。超出陣列末尾的記憶體可能包含草圖中使用的其他重要資料,然後我們的字串將覆蓋這些資料。如果超出字串末尾的記憶體超出,則可能會使草圖崩潰或導致意外行為。