如何在 Arduino 字符串的开头添加元素.类似于 JS unshift();

How to add elements at the beginning of an Arduino String. Similar to JS unshift();

本文关键字:JS 类似于 unshift 元素 添加 Arduino 字符串 开头      更新时间:2023-10-16

我有一个未知大小String,但最大大小为 8,我想通过在字符串开头添加 0(零(来确保其大小始终为 8。
例如,输入的String是"5687",我必须加 0 直到它的大小为 8:"00005687"。

String str = "5687";
while(str.length() < 8) {
// add 0 at the beginning of str
}

与JavaScript不同,Arduino C++没有像unshift((这样的东西,或者它有吗?

我怎样才能实现我的目标?谢谢。

PS:我使用的是 ESP32,只有几个字符串,因为 ESPAsyncWebServer 的处理程序通常会返回字符串参数。

String str = "5687";
while(str.length() < 8) {
str = String("0") + str;
}