从字符串c++/Arduino UNO删除字符

Remove character from String c++/Arduino UNO

本文关键字:UNO 删除 字符 Arduino 字符串 c++      更新时间:2023-10-16

我需要从字符串中删除一些字符。当我使用擦除它不工作,编译错误擦除没有成员命名。请帮帮我。可能是因为我写的是Arduino UNO

Arduino String class与std::string有很大不同。例如,erase不存在。但还有remove方法

无论如何,你应该从:https://www.arduino.cc/en/Reference/HomePage
开始

扩展KIIV的建议,您可以这样做:

void setup() {
    // put your setup code here, to run once:
    Serial.begin(9600);
}
void loop() {
    // put your main code here, to run repeatedly:
    String words = "This   is a sentence."; //reassign same string at the start of loop.
    delay(1000);
    Serial.println(words);
    char c;
    char no = ' '; //character I want removed.
    for (int i=0; i<words.length()-1;++i){
        c = words.charAt(i);
        if(c==no){
            words.remove(i, 1);
        }
    }
    Serial.println(words);
    delay(5000);//5 second delay, for demo purposes.
}

Arduino中的库是定制的,以便考虑目标微控制器的内存约束。例如,Uno运行在mega328P的Atmel(现在的Microchip)设备上,该设备只有32kb的闪存。