【问题标题】:How to split a string using a specific delimiter in Arduino?如何使用 Arduino 中的特定分隔符拆分字符串?
【发布时间】:2015-04-16 10:04:28
【问题描述】:

我有一个字符串变量,我想提取由 ; 分隔的三个子字符串。三个字符串变量。

String application_command = "{10,12; 4,5; 2}";

我不能使用子字符串方法,因为这个字符串也可以是以下任何一种或类似的模式。

String application_command = "{10,12,13,9,1; 4,5; 2}"

String application_command = "{7; 1,2,14; 1}"

在这些模式中唯一的共同点是由 ; 分隔的三个部分。

非常感谢任何见解。 谢谢

【问题讨论】:

    标签: arduino


    【解决方案1】:

    我认为您需要一个带有自定义分隔符的 split-string-into-string-array 函数。

    网络上和 stackoverflow 上已经有多个来源(例如 Split String into String array)。

    // https://stackoverflow.com/questions/9072320/split-string-into-string-array
    String getValue(String data, char separator, int index)
    {
      int found = 0;
      int strIndex[] = {0, -1};
      int maxIndex = data.length()-1;
    
      for(int i=0; i<=maxIndex && found<=index; i++){
        if(data.charAt(i)==separator || i==maxIndex){
            found++;
            strIndex[0] = strIndex[1]+1;
            strIndex[1] = (i == maxIndex) ? i+1 : i;
        }
      }
    
      return found>index ? data.substring(strIndex[0], strIndex[1]) : "";
    }
    

    您可以按如下方式使用此函数(以“;”作为分隔符):

    String part01 = getValue(application_command,';',0);
    String part02 = getValue(application_command,';',1);
    String part03 = getValue(application_command,';',2);
    

    编辑:更正单引号并在示例中添加分号。

    【讨论】:

    • 是的,我看到了之前的 stackoverflow 链接。您的解决方案只需稍作改动即可工作。字符串 part01 = getValue(application_command , ';' , 0);。我让它与你的解决方案和这个变化一起工作。感谢您的支持。
    • 你是对的。单引号对于第二个参数 (char) 是正确的,并且每一行当然必须以分号结尾。
    • 我知道这是一个旧帖子,但我喜欢它,但我需要对该功能进行一点扩展。我怎样才能添加到像 start_at_position 这样的函数参数看起来像这样: String getValue(String data, char separator, int startpos, int index) 谢谢!
    • 您可以使用 string.substring(from) 函数轻松实现这一点。这可以通过使用子字符串调用函数(例如,getValue(yourString.substring(yourStartPost), separator, index);)或通过使用附加参数扩展函数(例如,“data = data.substring (startpos) ;" 在函数的第一行)。子字符串的更多详细信息可在 Arduino 参考页面在线获得:arduino.cc/reference/en/language/variables/data-types/string/…
    【解决方案2】:

    新的 SafeString Arduino 库(可从库管理器获得)提供了许多标记化/子字符串方法,而没有 String 类的堆碎片


    https://www.forward.com.au/pfod/ArduinoProgramming/SafeString/index.html
    详细教程

    在这种情况下你可以使用

    #include "SafeString.h"
    
    void setup() {
      Serial.begin(9600);
    
      createSafeString(appCmd, 50);  // large enought for the largest cmd
      createSafeString(token1, 20);
      createSafeString(token2, 20);
      createSafeString(token3, 20);
      appCmd = "{10,12,13,9,1; 4,5; 2}";
      size_t nextIdx = 1; //step over leading {
      nextIdx = appCmd.stoken(token1, nextIdx, ";}");
      nextIdx++; //step over delimiter
      nextIdx = appCmd.stoken(token2, nextIdx, ";}");
      nextIdx++; //step over delimiter
      nextIdx = appCmd.stoken(token3, nextIdx, ";}");
      nextIdx++; //step over delimiter
      // can trim tokens if needed e.g. token1.trim()
      Serial.println(token1);
      Serial.println(token2); 
      Serial.println(token3);
    }
    
    void loop() {
    }
    

    另请查看 pfodParser,它解析这些类型的消息 { } 以供 pfodApp 使用。

    【讨论】:

      猜你喜欢
      • 2021-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多