Arduino:字符串连接?

发布于 2024-11-03 09:49:21 字数 601 浏览 3 评论 0原文

我正在尝试为 Arduino 编写一个 join 方法:

#define ARG_DELIMITER ','

  String join(const String strs[], const int len) {
    String result = "";
    for (int i = 0; i < len; i++) {
      result += strs[i] + ARG_DELIMITER;
      Serial.println(result);
    }
    return result.substring(0, result.length() - 1);
  }

loop() 中的调用代码:

const String args[3] = {"foo", "bar", "baz"};
Serial.println(SlaveTalk.join(args, 3));

这会打印以下内容:

foo
foo
foo
fo

只要程序运行,后跟空字符串。

我在这里做错了什么?

I'm trying to write a join method for the Arduino:

#define ARG_DELIMITER ','

  String join(const String strs[], const int len) {
    String result = "";
    for (int i = 0; i < len; i++) {
      result += strs[i] + ARG_DELIMITER;
      Serial.println(result);
    }
    return result.substring(0, result.length() - 1);
  }

The calling code in loop():

const String args[3] = {"foo", "bar", "baz"};
Serial.println(SlaveTalk.join(args, 3));

This prints the following:

foo
foo
foo
fo

followed by empty strings as long as the program runs.

What am I doing wrong here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

卖梦商人 2024-11-10 09:49:21

这一行

const String args[3] = {"foo", "bar", "baz"};

给出了字符串

"foo\0" "bar\0" "baz\0"

,其中 \0 是 NULL 字符。因此,当您连接时,我希望您最终会得到:

"foo\0bar\0baz\0"

打印在 null 处停止,这就是您看到 foo 3 次的原因。在 return 语句中,长度为 3“foo”,减去 1 得到“fo”

This line

const String args[3] = {"foo", "bar", "baz"};

gives you the strings

"foo\0" "bar\0" "baz\0"

where the \0 is the NULL character. So when you concat, I expect you're ending up with:

"foo\0bar\0baz\0"

The printing stops at the null which is why you see foo 3 times. In the return statement, the length is 3 "foo" and subtracting 1 gives you "fo"

坏尐絯℡ 2024-11-10 09:49:21

以下代码使用 Arduino 软件版本 0022 和带有 ATmega 328 的 Arduino Duemilanove 按预期工作:

#define ARG_DELIMITER ','

class SlaveTalkClass
{
  public:
    String join(const String strs[], const int len) {
      String result = "";
      for (int i = 0; i < len; i++) {
        result += strs[i] + ARG_DELIMITER;
        Serial.println(result);
      }
      return result.substring(0, result.length() - 1);
    }
} SlaveTalk;

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  const String args[3] = {"foo", "bar", "baz"};
  Serial.println(SlaveTalk.join(args, 3));
}

它会重复将以下内容打印到串行监视器:

foo,
foo,bar,
foo,bar,baz,
foo,bar,baz

The following code works as intended using the Arduino software version 0022 and an Arduino Duemilanove w/ ATmega 328:

#define ARG_DELIMITER ','

class SlaveTalkClass
{
  public:
    String join(const String strs[], const int len) {
      String result = "";
      for (int i = 0; i < len; i++) {
        result += strs[i] + ARG_DELIMITER;
        Serial.println(result);
      }
      return result.substring(0, result.length() - 1);
    }
} SlaveTalk;

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  const String args[3] = {"foo", "bar", "baz"};
  Serial.println(SlaveTalk.join(args, 3));
}

It repeatedly prints the following to the Serial Monitor:

foo,
foo,bar,
foo,bar,baz,
foo,bar,baz
小姐丶请自重 2024-11-10 09:49:21

PString 是一个优秀的可以连接的库。它有一些简洁的功能,最重要的是,它是运行时安全的。

PString is an excellent library that can concat. It has some neat features, and above all, is runtime safe.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文