如何在Arduino中刪除字串中的字元?
Arduino中的remove函式可以幫助你從字串中刪除一個或多個字元。
語法
myString.remove(index, count)
這裡,**索引**指的是刪除操作的起始位置。請注意,Arduino中的索引從0開始。因此,在字串“Hello”中,'H'的索引為0,'e'的索引為1,以此類推。
**計數**引數是可選的,它指定要刪除的字元數量。如果你不指定計數,則從索引開始到字串末尾的所有字元都將被刪除。如果你將計數指定為3,則將從索引位置開始刪除3個字元。
示例
void setup() { // put your setup code here, to run once: Serial.begin(9600); Serial.println(); String s1 = "Mississippi"; String s2 = "Mississippi"; String s3 = "Mississippi"; Serial.println(s1); Serial.println(s2); Serial.println(s3); Serial.println(); s1.remove(3,6); //Remove 6 characters starting from position 3 s2.remove(3); //Remove all characters starting from position 3 s3.remove(3,1); //Remove 1 character starting from position 3 Serial.println(s1); Serial.println(s2); Serial.println(s3); } void loop() { // put your main code here, to run repeatedly: }
輸出
序列埠監視器的輸出如下所示:
正如你所看到的,字元的刪除完全按照程式碼註釋中描述的那樣進行。
廣告