【问题标题】:Where can I find the library that provides String.substring() method? [closed]在哪里可以找到提供 String.substring() 方法的库? [关闭]
【发布时间】:2026-02-13 06:40:01
【问题描述】:

我正在尝试在 Linux 上编译一个 Arduino 项目,抽象出硬件部分。考虑以下行:

int keyNumRepeat = userInputPrev.substring(6, 8).toInt();

看起来 Arduino 使用了一些非标准库,我的系统上没有:

hsldz_totp_lock/hsldz_totp_lock.ino:335:38: error: ‘String’ {aka ‘class std::__cxx11::basic_string<char>’} has no member named ‘substring’; did you mean ‘substr’?
  335 |     int keyNumRepeat = userInputPrev.substring(6, 8).toInt();
      |                                      ^~~~~~~~~
      |                                      substr
make: *** [Makefile:54: sim] Error 1

它是否在某个地方可用,以便我可以在尝试编译时将其包含在我的项目中?还是严重依赖于其他 Arduino 实现细节?

【问题讨论】:

  • string::substr 不能满足您的需求吗?您所要做的就是将“substring”更改为“substr”,就像错误消息所暗示的那样。
  • @DM 注意这里还有 toInt 。我想我可以这样做,但我首先想尝试查看实际的库,否则我可能会得到很多 IFDEF
  • Arduino 的代码在 GitHub 上。
  • “安卓项目”?确定这不是 Java 代码?闻起来像 Java。
  • 对,这是一个错字。我的意思是 Arduino,而不是 Android。这是 C++ 代码,看起来他们重新实现了 String。

标签: c++ arduino porting


【解决方案1】:

看起来 Arduino 有一个 String 的自定义实现。

由于它是开源的,这里是headerclass 文件。我只是略过它们,但它们似乎并不严重依赖Arduino core API 的其余部分。

也就是说,您最好用标准的c++ 替换它们的实现。特别是如果它们只是生活质量的改善。使用string::substrstd::stoi 的等效代码应该是:

int keyNumRepeat = std::stoi(userInputPrev.substr(6, 8));

【讨论】:

    最近更新 更多